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

瀏覽器相容性

另見