此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

height

基线 广泛可用 *

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

* 此特性的某些部分的支持程度可能有所不同。

height CSS 属性指定了一个元素的高度。默认情况下,这个属性决定的是内容区( content area)的高度,但是,如果将 box-sizing 设置为 border-box , 这个属性决定的将是边框区域(border area)的高度。

尝试一下

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: #ffffff;
}

min-heightmax-height 属性会覆盖 height

语法

css
/* Keyword value */
height: auto;

/* <length> values */
height: 120px;
height: 10em;

/* <percentage> value */
height: 75%;

/* Global values */
height: inherit;
height: initial;
height: unset;

<length>

将高度定义为一个绝对值。

<percentage>

将高度定义为相对包含块高度的百分比。

border-box

如果设置该值,则 <length> 或者 <percentage> 会设置为该元素的 border box。

content-box

如果设置该值,则 <length> 或者 <percentage> 会设置为该元素的 content box。

auto

由浏览器为元素计算并选择一个高度。

fill

根据文字方向,使用 fill-available 作为行大小或者块大小。

max-content

设置为允许的最大高度。

min-content

设置为允许的最小高度。

available

包含块高度减去当前元素的边距,边框和填充。

fit-content

将 fill-content 公式中的可用位置替换为特定的参数以进行使用,如:min(max-content, max(min-content, ))

Formal syntax

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%;
}

结果

无障碍问题

确保设定了 height 的元素在显示上不会被截断,或者当页面放大时增大的字体大小不会遮挡其他内容。

规范

规范
CSS Box Sizing Module Level 3
# preferred-size-properties
CSS Box Sizing Module Level 4
# sizing-values
初始值auto
适用元素适用于所有元素,但不包括非替换行级元素、表格列和列组
是否是继承属性
PercentagesThe percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto. A percentage height on the root element is relative to the initial containing block.
计算值a percentage or auto or the absolute length
动画类型a length, percentage or calc();

浏览器兼容性

参见