height
Baseline 廣泛可用 *
height CSS 屬性用於指定元素的高度。預設情況下,該屬性定義內容區域的高度。但是,如果 box-sizing 設定為 border-box,則它將決定邊框區域的高度。
試一試
height: 150px;
height: 6em;
height: 75%;
height: auto;
<section class="default-example" id="default-example">
<div class="transition-all" id="example-element">
This is a box where you can change the height.
</div>
</section>
#example-element {
display: flex;
flex-direction: column;
background-color: #5b6dcd;
justify-content: center;
color: white;
}
min-height 和 max-height 屬性會覆蓋 height。
注意:作為幾何屬性,height 也適用於 <svg>、<rect>、<image> 和 <foreignObject> SVG 元素,其中 auto 解析為 0,百分比值相對於 <rect> 的 SVG 視口高度。CSS height 屬性值會覆蓋 SVG 元素上設定的任何 SVG height 屬性值。
語法
css
/* <length> values */
height: 120px;
height: 10em;
height: 100vh;
height: anchor-size(height);
height: anchor-size(--my-anchor self-block, 250px);
height: clamp(200px, anchor-size(width));
/* <percentage> value */
height: 75%;
/* Keyword values */
height: max-content;
height: min-content;
height: fit-content;
height: fit-content(20em);
height: auto;
height: stretch;
/* Global values */
height: inherit;
height: initial;
height: revert;
height: revert-layer;
height: unset;
值
<length>-
將高度定義為距離值。
<percentage>-
將高度定義為包含塊高度的百分比。
auto-
瀏覽器將計算併為指定元素選擇一個高度。
max-content-
固有的首選高度。
min-content-
固有的最小高度。
fit-content-
使用可用空間,但不超過 max-content,即
min(max-content, max(min-content, stretch))。 fit-content(<length-percentage>)-
使用 fit-content 公式,其中可用空間被指定引數替換,即
min(max-content, max(min-content, <length-percentage>))。 stretch-
將元素的外邊距框的高度設定為其包含塊的高度。它試圖使外邊距框填充包含塊中的可用空間,因此在某種程度上與
100%的行為相似,但將結果大小應用於外邊距框,而不是由 box-sizing 確定的框。
無障礙
確保在頁面放大以增加文字大小時,設定了 height 的元素不會被截斷和/或不會遮擋其他內容。
正式定義
正式語法
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
示例
使用畫素和百分比設定高度
HTML
html
<div id="taller">I'm 50 pixels tall.</div>
<div id="shorter">I'm 25 pixels tall.</div>
<div id="parent">
<div id="child">I'm half the height of my parent.</div>
</div>
CSS
css
div {
width: 250px;
margin-bottom: 5px;
border: 2px solid blue;
}
#taller {
height: 50px;
}
#shorter {
height: 25px;
}
#parent {
height: 100px;
}
#child {
height: 50%;
width: 75%;
}
結果
拉伸高度以填充包含塊
HTML
html
<div class="parent">
<div class="child">text</div>
</div>
<div class="parent">
<div class="child stretch">stretch</div>
</div>
CSS
css
.parent {
height: 150px;
margin: 1rem;
border: solid;
}
.child {
margin: 1rem;
background: #00999999;
}
.stretch {
height: stretch;
}
結果
規範
| 規範 |
|---|
| CSS Box Sizing Module Level 3 # preferred-size-properties |
| CSS Box Sizing Module Level 4 # sizing-values |
瀏覽器相容性
載入中…