width
CSS property sets an element's width. By default, it sets the width of the
content area
, but if
box-sizing
is set to
border-box
, it sets the width of the
border area
.
min-width
and
max-width
properties override
width
.
/* <length> values */ width: 300px; width: 25em; /* <percentage> value */ width: 75%; /* Keyword values */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* Global values */ width: inherit; width: initial; width: unset;
width
property is specified as either:
min-content
,
max-content
,
fit-content
,
auto
.
<length>
或
<percentage>
.
<length>
Defines the width as an absolute value.
<percentage>
Defines the width as a percentage of the containing block's width.
auto
The browser will calculate and select a width for the specified element.
max-content
The intrinsic preferred width.
min-content
The intrinsic minimum width.
fit-content(
<length-percentage>
)
min(max-content, max(min-content, <length-percentage>))
.
Ensure that elements set with a
width
aren't truncated and don't obscure other content when the page is zoomed to increase text size.
| 初始值 |
auto
|
|---|---|
| 适用于 | all elements but non-replaced inline elements, table rows, and row groups |
| 继承 | no |
| 百分比 | refer to the width of the containing block |
| 计算值 |
a percentage or
auto
or the absolute length
|
| 动画类型 | a length , percentage or calc(); |
auto | <length> | <percentage> | min-content | max-content | fit-content(<length-percentage>)where
<length-percentage> = <length> | <percentage>
p.goldie {
background: gold;
}
<p class="goldie">The Mozilla community produces a lot of great software.</p>
.px_length {
width: 200px;
background-color: red;
color: white;
border: 1px solid black;
}
.em_length {
width: 20em;
background-color: white;
color: red;
border: 1px solid black;
}
<div class="px_length">Width measured in px</div> <div class="em_length">Width measured in em</div>
.percent {
width: 20%;
background-color: silver;
border: 1px solid red;
}
<div class="percent">Width in percentage</div>
p.maxgreen {
background: lightgreen;
width: intrinsic; /* Safari/WebKit uses a non-standard name */
width: -moz-max-content; /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */
width: max-content;
}
<p class="maxgreen">The Mozilla community produces a lot of great software.</p>
p.minblue {
background: lightblue;
width: -moz-min-content; /* Firefox */
width: -webkit-min-content; /* Chrome */
width: min-content;
}
<p class="minblue">The Mozilla community produces a lot of great software.</p>
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Box Sizing Module Level 4
The definition of 'width' in that specification. |
编者草案 | |
|
CSS Box Sizing Module Level 3
The definition of 'width' in that specification. |
工作草案 |
添加
max-content
,
min-content
,
fit-content
keywords.
|
|
CSS Level 2 (Revision 1)
The definition of 'width' in that specification. |
推荐 | Precises on which element it applies to. |
|
CSS Level 1
The definition of 'width' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
width
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
| Animatable | Chrome 完整支持 26 | Edge 完整支持 12 | Firefox 完整支持 16 | IE 完整支持 11 | Opera 完整支持 15 | Safari 完整支持 6.1 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 26 | Firefox Android 完整支持 16 | Opera Android 完整支持 14 | Safari iOS 完整支持 6.1 | Samsung Internet Android 完整支持 1.5 |
fill
Experimental
非标
|
Chrome 完整支持 46 | Edge 完整支持 79 | Firefox 不支持 No | IE 不支持 No | Opera 完整支持 33 | Safari 完整支持 12 | WebView Android 完整支持 46 | Chrome Android 完整支持 46 | Firefox Android 不支持 No | Opera Android 完整支持 33 | Safari iOS 完整支持 12 | Samsung Internet Android 完整支持 5.0 |
fit-content
|
Chrome
完整支持
46
|
Edge 完整支持 79 |
Firefox
完整支持
3
Prefixed
|
IE 不支持 No |
Opera
完整支持
33
|
Safari
完整支持
11
|
WebView Android
完整支持
46
|
Chrome Android
完整支持
46
|
Firefox Android
完整支持
4
Prefixed
|
Opera Android
完整支持
33
|
Safari iOS
完整支持
11
|
Samsung Internet Android
完整支持
5.0
|
max-content
|
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
|
Chrome
完整支持
46
|
Edge 完整支持 79 |
Firefox
完整支持
66
|
IE 不支持 No |
Opera
完整支持
33
|
Safari
完整支持
11
|
WebView Android
完整支持
46
|
Chrome Android
完整支持
46
|
Firefox Android
完整支持
66
|
Opera Android
完整支持
33
|
Safari iOS
完整支持
11
|
Samsung Internet Android
完整支持
5.0
|
stretch
Experimental
|
Chrome
完整支持
22
Alternate Name
|
Edge
完整支持
79
Alternate Name
|
Firefox
完整支持
3
Alternate Name
|
IE 不支持 No |
Opera
完整支持
15
Alternate Name
|
Safari
完整支持
6.1
Alternate Name
|
WebView Android
完整支持
4.4
Alternate Name
|
Chrome Android
完整支持
25
Alternate Name
|
Firefox Android
完整支持
4
Alternate Name
|
Opera Android
完整支持
14
Alternate Name
|
Safari iOS
完整支持
6.1
Alternate Name
|
Samsung Internet Android
完整支持
5.0
Alternate Name
|
完整支持
不支持
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
使用非标名称。
要求使用供应商前缀或不同名称。
height
box-sizing
min-width
,
max-width
block-size
,
inline-size
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