大网数据广告活动
Web 标准与前端开发

height

· 52CE 我爱测

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

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

CSS Demo: height

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-height 和 max-height 属性会覆盖 height。

语法

/* 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

示例

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

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 的元素在显示上不会被截断,或者当页面放大时增大的字体大小不会遮挡其他内容。

规范

浏览器兼容性

参见

  • box model, width, box-sizing, min-height, max-height

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。