Skip to content
全部文档

max-height

用于设置元素最大高度的工具类。

快速参考

属性
max-h-<number>max-height: calc(var(--spacing) * <number>);
max-h-<fraction>max-height: calc(<fraction> * 100%);
max-h-nonemax-height: none;
max-h-pxmax-height: 1px;
max-h-fullmax-height: 100%;
max-h-screenmax-height: 100vh;
max-h-dvhmax-height: 100dvh;
max-h-dvwmax-height: 100dvw;
max-h-lvhmax-height: 100lvh;
max-h-lvwmax-height: 100lvw;
max-h-svhmax-height: 100svh;
max-h-svwmax-height: 100svw;
max-h-minmax-height: min-content;
max-h-maxmax-height: max-content;
max-h-fitmax-height: fit-content;
max-h-lhmax-height: 1lh;
max-h-(<custom-property>)max-height: var(<custom-property>);
max-h-[<value>]max-height: <value>;

示例

基础示例

使用 max-h-<number> 这类工具类(如 max-h-24max-h-64)按间距比例将元素设为固定最大高度:

max-h-80
max-h-64
max-h-48
max-h-40
max-h-32
max-h-24
html
<!-- [!code classes:max-h-80,max-h-64,max-h-48,max-h-40,max-h-32,max-h-24,max-h-full] -->
<div class="h-96 ...">
  <div class="h-full max-h-80 ...">max-h-80</div>
  <div class="h-full max-h-64 ...">max-h-64</div>
  <div class="h-full max-h-48 ...">max-h-48</div>
  <div class="h-full max-h-40 ...">max-h-40</div>
  <div class="h-full max-h-32 ...">max-h-32</div>
  <div class="h-full max-h-24 ...">max-h-24</div>
</div>

使用百分比

使用 max-h-fullmax-h-<fraction> 这类工具类(如 max-h-1/2max-h-2/5)为元素设置基于百分比的最大高度:

max-h-9/10
max-h-3/4
max-h-1/2
max-h-1/4
max-h-full
html
<!-- [!code classes:max-h-9/10,max-h-3/4,max-h-1/2,max-h-1/4,max-h-full] -->
<div class="h-96 ...">
  <div class="h-full max-h-9/10 ...">max-h-9/10</div>
  <div class="h-full max-h-3/4 ...">max-h-3/4</div>
  <div class="h-full max-h-1/2 ...">max-h-1/2</div>
  <div class="h-full max-h-1/4 ...">max-h-1/4</div>
  <div class="h-full max-h-full ...">max-h-full</div>
</div>

使用自定义值

使用 max-h-[<value>] 这类工具类,按完全自定义的值设置最大高度:

html
<div class="max-h-[220px] ...">
 <!-- ... -->
</div>

对于 CSS 变量,也可以使用 max-h-(<custom-property>) 语法:

html
<div class="max-h-(--my-max-height) ...">
 <!-- ... -->
</div>

这只是 max-h-[var(<custom-property>)] 的简写,会自动帮你加上 var() 函数。

响应式设计

max-height 工具类前加上 md: 这样的断点变体,即可仅在中等及以上屏幕尺寸下应用该工具类:

html
<div class="h-48 max-h-full md:max-h-screen ...">
 <!-- ... -->
</div>

变体文档 中了解更多关于使用变体的信息。

自定义主题

max-h-<number> 工具类由 --spacing 主题变量驱动,你可以在自己的主题中自定义它:

css
@theme {
  --spacing: 1px;
}

主题变量文档 中了解如何自定义间距比例。

文档译文以 MIT 协议授权;原文版权归 Tailwind Labs。湘ICP备2026005453号-2