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

max-height

· 52CE 我爱测

CSS 属性 **`max-height`** 设置元素的最大高度。它防止 height 属性的应用值大于 `max-height` 指定的值。

CSS 属性 max-height 设置元素的最大高度。它防止 height 属性的应用值大于 max-height 指定的值。

CSS Demo: max-height

max-height: 150px;
max-height: 7em;
max-height: 75%;
max-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 maximum height. <br />This will limit
    how tall the box can be, potentially causing an overflow.
  </div>
</section>
#example-element {
  display: flex;
  flex-direction: column;
  background-color: #5b6dcd;
  justify-content: center;
  color: #ffffff;
}

max-height 会覆盖 height,而 min-height 会覆盖 max-height。

语法

/* <length> 值 */
max-height: 3.5em;

/* <percentage> 值 */
max-height: 75%;

/* 关键字值 */
max-height: none;
max-height: max-content;
max-height: min-content;
max-height: fit-content;
max-height: fit-content(20em);

/* 全局值 */
max-height: inherit;
max-height: initial;
max-height: revert;
max-height: revert-layer;
max-height: unset;

值

  • <length>
    • : 定义作为绝对值的 max-height。
  • <percentage>
    • : 定义作为包含区块高度百分比的 max-height。
  • none
    • : 盒子大小没有限制。
  • max-content
    • : 固有的首选 max-height。
  • min-content
    • : 固有的最小 max-height。
  • fit-content(&lt;length-percentage&gt;)
    • : 使用 fit-content 公式,用指定参数替换可用空间,即 min(max-content, max(min-content, argument))。

无障碍考虑

确保设置了 max-height 的元素在页面缩放以增大文字大小时不会被截断和/或遮挡其他内容。

形式定义

形式语法

示例

使用百分比和关键字值设置 max-height

table {
  max-height: 75%;
}

form {
  max-height: none;
}

规范

浏览器兼容性

参见

  • 盒模型、box-sizing
  • height、min-height
  • 映射的逻辑属性:max-inline-size、max-block-size

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

资料来源与适用范围

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

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

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