试一试
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: white;
}
max-height 会覆盖 height,但 min-height 会覆盖 max-height。
语法
css
/* <length> value */
max-height: 3.5em;
max-height: anchor-size(height);
max-height: calc(anchor-size(--my-anchor self-block, 250px) + 2em);
/* <percentage> value */
max-height: 75%;
/* Keyword values */
max-height: none;
max-height: max-content;
max-height: min-content;
max-height: fit-content;
max-height: fit-content(20em);
max-height: stretch;
/* Global values */
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-
使用可用空间,但不超过 max-content,即
min(max-content, max(min-content, stretch))。 fit-content(<length-percentage>)-
使用
fit-content公式,并将可用空间替换为指定参数,即min(max-content, max(min-content, argument))。 stretch-
将元素外边距框的最大高度限制为其包含块的高度。它试图让外边距框填充包含块中的可用空间,因此在某种程度上行为类似于
100%,但将结果大小应用于外边距框,而不是由 box-sizing 决定的框。注意:要查看浏览器用于
stretch值的别名及其实现状态,请参阅浏览器兼容性部分。
无障碍
确保当页面放大以增加文本大小时,设置了 max-height 的元素不会被截断和/或不会遮挡其他内容。
正式定义
正式语法
max-height =
none |
<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
示例
使用百分比和关键字值设置 max-height
css
table {
max-height: 75%;
}
form {
max-height: none;
}
规范
| 规范 |
|---|
| CSS Box Sizing Module Level 4 # sizing-values |
浏览器兼容性
加载中…