max-content
max-content 尺寸关键字表示元素的最大固有尺寸。该关键字将元素扩展到显示其内容所需的、不进行任何软换行的最大尺寸。对于文本内容,此关键字根本不会换行,即使这会导致溢出。
interpolate-size 属性和 calc-size() 函数可用于启用到和从 max-content 的动画。
语法
css
/* Used as a length value */
width: max-content;
inline-size: max-content;
height: max-content;
block-size: max-content;
/* Used in grid tracks */
grid-template-columns: 200px 1fr max-content;
示例
使用 max-content 调整盒子尺寸
HTML
html
<div id="container">
<div class="item">Item</div>
<div class="item">
Item with more text in it which will overflow the fixed width box.
</div>
</div>
CSS
css
#container {
background-color: #8cffa0;
padding: 10px;
width: 200px;
}
.item {
width: max-content;
background-color: #8ca0ff;
padding: 5px;
margin-bottom: 1em;
}
结果
使用 max-content 调整网格列尺寸
HTML
html
<div id="container">
<div>Item</div>
<div>Item with more text in it.</div>
<div>Flexible item</div>
</div>
CSS
css
#container {
display: grid;
grid-template-columns: max-content max-content 1fr;
grid-gap: 5px;
box-sizing: border-box;
height: 200px;
width: 100%;
background-color: #8cffa0;
padding: 10px;
}
#container > div {
background-color: #8ca0ff;
padding: 5px;
}
结果
规范
| 规范 |
|---|
| CSS Box Sizing Module Level 3 # valdef-width-max-content |
浏览器兼容性
加载中…
另见
- 相关的尺寸关键字:
min-content、fit-content - CSS 盒模型尺寸模块