min-height

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

min-height CSS 属性设置元素的最小高度。它阻止 height 属性的使用值变得小于为 min-height 指定的值。

试一试

min-height: 150px;
min-height: 7em;
min-height: 75%;
min-height: 10px;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box where you can change the minimum height. <br />If there is
    more content than the minimum the box will grow to the height needed by the
    content.
  </div>
</section>
#example-element {
  display: flex;
  flex-direction: column;
  background-color: #5b6dcd;
  justify-content: center;
  color: white;
}

min-height 大于 max-heightheight 时,元素的 height 将被设置为 min-height 的值。

语法

css
/* <length> value */
min-height: 3.5em;
min-height: anchor-size(height);
min-height: anchor-size(--my-anchor block, 200px);

/* <percentage> value */
min-height: 10%;

/* Keyword values */
min-height: max-content;
min-height: min-content;
min-height: fit-content;
min-height: fit-content(20em);
min-height: stretch;

/* Global values */
min-height: inherit;
min-height: initial;
min-height: revert;
min-height: revert-layer;
min-height: unset;

<length>

min-height 定义为绝对值。

<percentage>

min-height 定义为包含块高度的百分比。

auto

浏览器将为指定元素计算并选择一个 min-height

max-content

固有的首选 min-height

min-content

固有的最小 min-height

fit-content

使用可用空间,但不超过 max-content,即 min(max-content, max(min-content, stretch))

fit-content(<length-percentage>)

使用 fit-content 公式,并将可用空间替换为指定参数,即 min(max-content, max(min-content, argument))

stretch

将元素的外边距框的最小高度限制为其包含块的高度。它试图使外边距框填充包含块中的可用空间,因此在某种程度上,它的行为类似于 100%,但将结果大小应用于外边距框,而不是由 box-sizing 确定的框。

注意:要查看浏览器用于 stretch 值的别名及其实现状态,请参阅浏览器兼容性部分。

正式定义

初始值auto
应用于除非替换行内元素、表格列和列组之外的所有元素
继承性
百分比百分比是相对于生成的框的包含块的高度计算的。如果包含块的高度未明确指定(即,它取决于内容高度),并且此元素不是绝对定位的,则百分比值被视为 0
计算值指定的百分比或绝对长度
动画类型一个长度百分比或 calc();

正式语法

min-height = 
auto |
<length-percentage [0,∞]> |
min-content |
max-content |
fit-content( <length-percentage [0,∞]> ) |
<calc-size()> |
<anchor-size()> |
stretch |
fit-content |
contain

<length-percentage> =
<length> |
<percentage>

<calc-size()> =
calc-size( <calc-size-basis> , <calc-sum> )

<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

<calc-size-basis> =
<size-keyword> |
<calc-size()> |
any |
<calc-sum>

<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*

<anchor-name> =
<dashed-ident>

<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline

<calc-product> =
<calc-value> [ [ '*' | / ] <calc-value> ]*

<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-keyword> |
( <calc-sum> )

<calc-keyword> =
e |
pi |
infinity |
-infinity |
NaN

示例

设置 min-height

css
table {
  min-height: 75%;
}

form {
  min-height: 0;
}

规范

规范
CSS Box Sizing Module Level 4
# sizing-values

浏览器兼容性

另见