max-width
CSS
property sets the maximum width of an element. It prevents the
used value
of the
width
property from becoming larger than the value specified by
max-width
.
max-width
overrides
width
, but
min-width
overrides
max-width
.
/* <length> value */ max-width: 3.5em; /* <percentage> value */ max-width: 75%; /* Keyword values */ max-width: none; max-width: max-content; max-width: min-content; max-width: fit-content(20em); /* Global values */ max-width: inherit; max-width: initial; max-width: unset;
<length>
max-width
as an absolute value.
<percentage>
max-width
as a percentage of the containing block's width.
none
No limit on the size of the box.
max-content
max-width
.
min-content
max-width
.
fit-content(
<length-percentage>
)
fit-content
formula with the available space replaced by the specified argument, i.e.
min(max-content, max(min-content,
argument
))
.
Ensure that elements set with a
max-width
are not truncated and/or do not obscure other content when the page is zoomed to increase text size.
| 初始值 |
none
|
|---|---|
| 适用于 | all elements but non-replaced inline elements, table rows, and row groups |
| 继承 | no |
| 百分比 | refer to the width of the containing block |
| 计算值 |
the percentage as specified or the absolute length or
none
|
| 动画类型 | a length , percentage or calc(); |
auto | <length> | <percentage> | min-content | max-content | fit-content(<length-percentage>)where
<length-percentage> = <length> | <percentage>
In this example, the "child" will be either 150 pixels wide or the width of the "parent," whichever is smaller.
<div id="parent">
<div id="child">
Fusce pulvinar vestibulum eros, sed luctus ex lobortis quis.
</div>
</div>
#parent {
background: lightblue;
width: 300px;
}
#child {
background: gold;
width: 100%;
max-width: 150px;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Box Sizing Module Level 4
The definition of 'max-width' in that specification. |
编者草案 | |
|
CSS Box Sizing Module Level 3
The definition of 'max-width' in that specification. |
工作草案 |
添加
max-content
,
min-content
,
fit-content
keywords.
|
|
CSS Level 2 (Revision 1)
The definition of 'max-width' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
max-width
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
1
注意事项
|
IE 完整支持 7 |
Opera
完整支持
4
注意事项
|
Safari 完整支持 1 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 18 |
Firefox Android
完整支持
4
注意事项
|
Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
fit-content
Experimental
|
Chrome 完整支持 46 | Edge 完整支持 79 |
Firefox
部分支持
3
Prefixed
注意事项
|
IE 不支持 No | Opera 完整支持 44 |
Safari
完整支持
11
|
WebView Android 完整支持 46 | Chrome Android 完整支持 46 |
Firefox Android
部分支持
4
Prefixed
注意事项
|
Opera Android 完整支持 43 |
Safari iOS
完整支持
11
|
Samsung Internet Android 完整支持 5.0 |
max-content
Experimental
|
Chrome 完整支持 46 | Edge 完整支持 79 |
Firefox
完整支持
66
|
IE 不支持 No | Opera 完整支持 44 |
Safari
完整支持
11
|
WebView Android 完整支持 46 | Chrome Android 完整支持 46 |
Firefox Android
完整支持
66
|
Opera Android 完整支持 43 |
Safari iOS
完整支持
11
|
Samsung Internet Android 完整支持 5.0 |
min-content
Experimental
|
Chrome 完整支持 46 | Edge 完整支持 79 |
Firefox
完整支持
66
|
IE 不支持 No | Opera 完整支持 44 |
Safari
完整支持
11
|
WebView Android 完整支持 46 | Chrome Android 完整支持 46 |
Firefox Android
完整支持
66
|
Opera Android 完整支持 43 |
Safari iOS
完整支持
11
|
Samsung Internet Android 完整支持 5.0 |
stretch
Experimental
|
Chrome
完整支持
22
Alternate Name
|
Edge
完整支持
79
Alternate Name
|
Firefox 不支持 No | IE 不支持 No |
Opera
完整支持
15
Alternate Name
|
Safari 不支持 No |
WebView Android
完整支持
4.4
Alternate Name
|
Chrome Android
完整支持
25
Alternate Name
|
Firefox Android 不支持 No |
Opera Android
完整支持
14
Alternate Name
|
Safari iOS 不支持 No |
Samsung Internet Android
完整支持
1.5
Alternate Name
|
完整支持
部分支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
使用非标名称。
要求使用供应商前缀或不同名称。
height
margin-bottom
margin-left
margin-right
margin-top
margin-trim
max-height
max-width
min-height
min-width
overscroll-behavior
overscroll-behavior-block
overscroll-behavior-inline
overscroll-behavior-x
overscroll-behavior-y
padding
padding-bottom
padding-left
padding-right
padding-top
visibility
width