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 .

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

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>
Defines the max-width as an absolute value.
<percentage>
Defines the max-width as a percentage of the containing block's width.
none

No limit on the size of the box.

max-content
The intrinsic preferred max-width .
min-content
The intrinsic minimum max-width .
fit-content( <length-percentage> )
Uses the 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>

范例

Setting max width in pixels

In this example, the "child" will be either 150 pixels wide or the width of the "parent," whichever is smaller.

HTML

<div id="parent">
  <div id="child">
    Fusce pulvinar vestibulum eros, sed luctus ex lobortis quis.
  </div>
</div>
					

CSS

#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.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
max-width Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 1 注意事项
完整支持 1 注意事项
注意事项 CSS 2.1 leaves the behavior of max-width with table undefined. Firefox supports applying max-width to table elements.
IE 完整支持 7 Opera 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 CSS 2.1 leaves the behavior of max-width with table undefined. Opera supports applying max-width to table elements.
Safari 完整支持 1 WebView Android 完整支持 4.4 Chrome Android 完整支持 18 Firefox Android 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 CSS 2.1 leaves the behavior of max-width with table undefined. Firefox supports applying max-width to table elements.
Opera Android 完整支持 14 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
fit-content Experimental Chrome 完整支持 46 Edge 完整支持 79 Firefox 部分支持 3 Prefixed 注意事项
部分支持 3 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 Firefox implements the definitions given in CSS3 Basic Box. This defines available and not fit-available . Also, the definition of fit-content is simpler than in CSS3 Sizing.
IE 不支持 No Opera 完整支持 44 Safari 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 2 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
WebView Android 完整支持 46 Chrome Android 完整支持 46 Firefox Android 部分支持 4 Prefixed 注意事项
部分支持 4 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 Firefox implements the definitions given in CSS3 Basic Box. This defines available and not fit-available . Also, the definition of fit-content is simpler than in CSS3 Sizing.
Opera Android 完整支持 43 Safari iOS 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Samsung Internet Android 完整支持 5.0
max-content Experimental Chrome 完整支持 46 Edge 完整支持 79 Firefox 完整支持 66
完整支持 66
完整支持 3 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 不支持 No Opera 完整支持 44 Safari 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 2 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
WebView Android 完整支持 46 Chrome Android 完整支持 46 Firefox Android 完整支持 66
完整支持 66
完整支持 4 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 43 Safari iOS 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Samsung Internet Android 完整支持 5.0
min-content Experimental Chrome 完整支持 46 Edge 完整支持 79 Firefox 完整支持 66
完整支持 66
完整支持 3 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 不支持 No Opera 完整支持 44 Safari 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 2 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
WebView Android 完整支持 46 Chrome Android 完整支持 46 Firefox Android 完整支持 66
完整支持 66
完整支持 4 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 43 Safari iOS 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Samsung Internet Android 完整支持 5.0
stretch Experimental Chrome 完整支持 22 Alternate Name
完整支持 22 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Edge 完整支持 79 Alternate Name
完整支持 79 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Firefox 不支持 No IE 不支持 No Opera 完整支持 15 Alternate Name
完整支持 15 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Safari 不支持 No WebView Android 完整支持 4.4 Alternate Name
完整支持 4.4 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Chrome Android 完整支持 25 Alternate Name
完整支持 25 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Firefox Android 不支持 No Opera Android 完整支持 14 Alternate Name
完整支持 14 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available
Safari iOS 不支持 No Samsung Internet Android 完整支持 1.5 Alternate Name
完整支持 1.5 Alternate Name
Alternate Name Uses the non-standard name: -webkit-fill-available

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

见实现注意事项。

使用非标名称。

使用非标名称。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: