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(<length-percentage>)- : 使用
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。离线整理了版式、链接和媒体显示。
