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 |
浏览器兼容性
加载中…