display
CSS
property sets whether an element is treated as a
block or inline element
and the layout used for its children, such as
flow layout
,
grid
or
flex
.
Formally, the
display
property sets an element's inner and outer
display types
. The outer type sets an element's participation in
flow layout
; the inner type sets the layout of children. Some values of
display
are fully defined in their own individual specifications; for example the detail of what happens when
display: flex
is declared is defined in the CSS Flexible Box Model specification. See
the table at the end of this document
for all of the individual specifications.
The CSS
display
property is specified using keyword values. Keyword values are grouped into six value categories:
.container {
display: [ <display-outside> | <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy> ;
}
<display-outside>
These keywords specify the element’s outer display type, which is essentially its role in flow layout.
Valid
<display-outside>
values:
block
The element generates a block element box, generating line breaks both before and after the element when in the normal flow.
inline
The element generates one or more inline element boxes that do not generate line breaks before or after themselves. In normal flow, the next element will be on the same line if there is space
run-in
display: run-in
box is a block box, the
run-in
box becomes the first inline box of the block box that follows it.
注意
: Browsers that support the two value syntax, on finding the outer value only, such as when
display: block
or
display: inline
is specified, will set the inner value to
flow
. This will result in expected behavior; for example if you specify an element to be block, you would expect that the children of that element would participate in block and inline normal flow layout.
<display-inside>
These keywords specify the element’s inner display type, which defines the type of formatting context that its contents are laid out in (assuming it is a non-replaced element).
Valid
<display-inside>
values:
flow
If its outer display type is
inline
or
run-in
, and it is participating in a block or inline formatting context, then it generates an inline box. Otherwise it generates a block container box.
Depending on the value of other properties (such as
position
,
float
,或
overflow
) and whether it is itself participating in a block or inline formatting context, it either establishes a new
block formatting context
(BFC) for its contents or integrates its contents into its parent formatting context.
flow-root
table
<table>
elements. It defines a block-level box.
flex
grid
ruby
<ruby>
elements.
注意
: Browsers that support the two value syntax, on finding the inner value only, such as when
display: flex
or
display: grid
is specified, will set their outer value to
block
. This will result in expected behavior; for example if you specify an element to be
display: grid
, you would expect that the box created on the grid container would be a block level box.
<display-listitem>
The element generates a block box for the content and a separate list-item inline box.
A single value of
list-item
will cause the element to behave like a list item. This can be used together with
list-style-type
and
list-style-position
.
list-item
can also be combined with any
<display-outside>
keyword and the
flow
or
flow-root
<display-inside>
keywords.
注意
: In browsers that support the two-value syntax, if no inner value is specified it will default to
flow
. If no outer value is specified, the principal box will have an outer display type of
block
.
<display-internal>
table
and
ruby
have a complex internal structure, with several different roles that their children and descendants can fill. This section defines those "internal" display values, which only have meaning within that particular layout mode.
Valid
<display-internal>
values:
table-row-group
<tbody>
HTML elements.
table-header-group
<thead>
HTML elements.
table-footer-group
<tfoot>
HTML elements.
table-row
<tr>
HTML elements.
table-cell
<td>
HTML elements.
table-column-group
<colgroup>
HTML elements.
table-column
<col>
HTML elements.
table-caption
<caption>
HTML elements.
ruby-base
<rb>
HTML elements.
ruby-text
<rt>
HTML elements.
ruby-base-container
<rbc>
HTML elements generated as anonymous boxes.
ruby-text-container
<rtc>
HTML elements.
<display-box>
These values define whether an element generates display boxes at all.
Valid
<display-box>
values:
contents
contents
value should affect "unusual elements" — elements that aren’t rendered purely by CSS box concepts such as replaced elements. See
Appendix B: Effects of display: contents on Unusual Elements
了解更多细节。
none
visibility
特性代替。
<display-legacy>
display
property, requiring separate keywords for block-level and inline-level variants of the same layout mode.
Valid
<display-legacy>
values:
inline-block
inline flow-root
.
inline-table
inline-table
value does not have a direct mapping in HTML. It behaves like an HTML
<table>
element, but as an inline box, rather than a block-level box. Inside the table box is a block-level context.
inline table
.
inline-flex
inline flex
.
inline-grid
inline grid
.
The Level 3 specification details two values for the
display
property — enabling the specification of the outer and inner display type explicitly — but this is not yet well-supported by browsers.
<display-legacy>
methods allow the same results with single keyword values, and should be favoured by developers until the two keyword values are better supported. For example, using two values you might specify an inline flex container as follows:
.container {
display: inline flex;
}
This can currently be specified using a single value.
.container {
display: inline-flex;
}
For more information on these changes to the specification, see the article Adapting to the new two-value syntax of display .
/* Global values */ display: inherit; display: initial; display: unset;
The individual pages for the different types of value that
display
can have set on it feature multiple examples of those values in action — see the
句法
section. In addition, see the following material, which covers the various values of display in depth.
Using a
display
value of
none
on an element will remove it from the
accessibility tree
. This will cause the element and all its descendant elements to no longer be announced by screen reading technology.
If you want to visually hide the element, a more accessible alternative is to use a combination of properties to remove it visually from the screen but keep it parseable by assistive technology such as screen readers.
Current implementations in most browsers will remove from the
accessibility tree
any element with a
display
value of
contents
(but descendants will remain). This will cause the element itself to no longer be announced by screen reading technology. This is incorrect behavior according to the
CSS specification
.
Changing the
display
value of a
<table>
element to
block
,
grid
,或
flex
will alter its representation in the
accessibility tree
. This will cause the table to no longer be announced properly by screen reading technology.
| 初始值 |
inline
|
|---|---|
| 适用于 | 所有元素 |
| 继承 | no |
| 计算值 | as the specified value, except for positioned and floating elements and the root element. In both cases the computed value may be a keyword other than the one specified. |
| 动画类型 | discrete |
[ <display-outside> || <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy>where
<display-outside> = block | inline | run-in
<display-inside> = flow | flow-root | table | flex | grid | ruby
<display-listitem> = <display-outside>? && [ flow | flow-root ]? && list-item
<display-internal> = table-row-group | table-header-group | table-footer-group | table-row | table-cell | table-column-group | table-column | table-caption | ruby-base | ruby-text | ruby-base-container | ruby-text-container
<display-box> = contents | none
<display-legacy> = inline-block | inline-list-item | inline-table | inline-flex | inline-grid
In this example we have two block-level container elements, each one with three inline children. Below that, we have a select menu that allows you to apply different
display
values to the containers, allowing you to compare and contrast how the different values affect the element's layout, and that of their children.
We've included
padding
and
background-color
on the containers and their children, so that it is easier to see the effect the display values are having.
注意 : We've not included any of the modern two-value syntax, as support for that is still fairly limited.
<article class="container">
<span>First</span>
<span>Second</span>
<span>Third</span>
</article>
<article class="container">
<span>First</span>
<span>Second</span>
<span>Third</span>
</article>
<div>
<label for="display">Choose a display value:</label>
<select id="display">
<option selected>block</option>
<option>inline</option>
<option>inline-block</option>
<option>none</option>
<option>flex</option>
<option>inline-flex</option>
<option>grid</option>
<option>inline-grid</option>
<option>table</option>
<option>list-item</option>
</select>
</div>
html {
font-family: helvetica, arial, sans-serif;
letter-spacing: 1px;
padding-top: 10px;
}
article {
background-color: red;
}
article span {
background-color: black;
color: white;
margin: 1px;
}
article, span {
padding: 10px;
border-radius: 7px;
}
article, div {
margin: 20px;
}
const articles = document.querySelectorAll('.container');
const select = document.querySelector('select');
function updateDisplay() {
articles.forEach((article) => {
article.style.display = select.value;
});
}
select.addEventListener('change', updateDisplay);
updateDisplay();
注意 : You can find more examples in the pages for each separate display data type, linked above.
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Display Module Level 3
The definition of 'display' in that specification. |
候选推荐 |
添加
run-in
,
flow
,
flow-root
,
contents
, and multi-keyword values.
|
|
CSS Ruby Layout Module Level 1
The definition of 'display' in that specification. |
工作草案 |
添加
ruby
,
ruby-base
,
ruby-text
,
ruby-base-container
,和
ruby-text-container
.
|
|
CSS 栅格布局
The definition of 'display' in that specification. |
候选推荐 | Added the grid box model values. |
|
CSS Flexible Box Layout Module
The definition of 'display' in that specification. |
候选推荐 | Added the flexible box model values. |
|
CSS Level 2 (Revision 1)
The definition of 'display' in that specification. |
推荐 |
Added the table model values and
inline-block
.
|
|
CSS Level 1
The definition of 'display' in that specification. |
推荐 |
Initial definition. Basic values:
none
,
block
,
inline
,和
list-item
.
|
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
display
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
contents
|
Chrome
完整支持
65
|
Edge 完整支持 79 |
Firefox
完整支持
37
|
IE 不支持 No |
Opera
完整支持
52
|
Safari 完整支持 11.1 | WebView Android 完整支持 65 |
Chrome Android
完整支持
65
|
Firefox Android 完整支持 57 |
Opera Android
完整支持
47
|
Safari iOS 完整支持 11.3 | Samsung Internet Android 完整支持 9.0 |
contents
: Specific behavior of
unusual elements
when
display: contents
is applied to them
|
Chrome 完整支持 58 | Edge 完整支持 79 | Firefox 完整支持 59 | IE 不支持 No | Opera 完整支持 45 | Safari 不支持 No | WebView Android 完整支持 65 | Chrome Android 完整支持 58 | Firefox Android 完整支持 59 | Opera Android 完整支持 43 | Safari iOS 不支持 No | Samsung Internet Android 完整支持 9.0 |
<display-outside>
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
display-outside.run-in
Experimental
|
Chrome
不支持
1 — 32
注意事项
|
Edge 不支持 No | Firefox 不支持 No | IE 完整支持 8 | Opera 不支持 7 — 19 |
Safari
不支持
1 — 8
注意事项
|
WebView Android 不支持 ≤37 — ≤37 | Chrome Android 不支持 18 — 32 | Firefox Android 不支持 No | Opera Android 不支持 10.1 — 19 |
Safari iOS
不支持
1 — 8
注意事项
|
Samsung Internet Android 不支持 1.0 — 2.0 |
flex
|
Chrome 完整支持 29 | Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE
部分支持
11
注意事项
|
Opera
完整支持
16
|
Safari 完整支持 9 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 29 |
Firefox Android
完整支持
20
注意事项
|
Opera Android
完整支持
16
|
Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 2.0 |
flow-root
|
Chrome 完整支持 58 | Edge 完整支持 79 | Firefox 完整支持 53 | IE 不支持 No | Opera 完整支持 45 | Safari 完整支持 13 | WebView Android 完整支持 58 | Chrome Android 完整支持 58 | Firefox Android 完整支持 53 | Opera Android 完整支持 43 | Safari iOS 完整支持 13 | Samsung Internet Android 完整支持 7.0 |
grid
|
Chrome 完整支持 57 |
Edge
完整支持
16
|
Firefox 完整支持 52 |
IE
部分支持
10
Prefixed
注意事项
|
Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 52 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 |
Samsung Internet Android
完整支持
6.0
注意事项
|
inline-block
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 |
IE
完整支持
8
|
Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
inline-flex
|
Chrome 完整支持 29 | Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE
完整支持
11
|
Opera 完整支持 16 | Safari 完整支持 9 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 29 |
Firefox Android
完整支持
20
注意事项
|
Opera Android 完整支持 16 | Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 2.0 |
inline-grid
|
Chrome 完整支持 57 |
Edge
完整支持
16
|
Firefox 完整支持 52 |
IE
部分支持
10
Prefixed
注意事项
|
Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 52 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 |
Samsung Internet Android
完整支持
6.0
注意事项
|
inline-table
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 3 | IE 完整支持 8 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
list-item
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 6 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
list-item
: Supported on
<legend>
|
Chrome 完整支持 71 | Edge 完整支持 79 | Firefox 完整支持 64 | IE 不支持 No | Opera 完整支持 58 | Safari 不支持 No | WebView Android 完整支持 71 | Chrome Android 完整支持 71 | Firefox Android 完整支持 64 | Opera Android 完整支持 50 | Safari iOS 不支持 No | Samsung Internet Android 完整支持 10.0 |
| Multi-keyword values Experimental | Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 70 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
none
|
Chrome
完整支持
1
注意事项
|
Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 |
Opera
完整支持
7
注意事项
|
Safari 完整支持 1 |
WebView Android
完整支持
≤37
注意事项
|
Chrome Android
完整支持
18
注意事项
|
Firefox Android 完整支持 4 |
Opera Android
完整支持
10.1
注意事项
|
Safari iOS 完整支持 1 |
Samsung Internet Android
完整支持
1.0
注意事项
|
ruby
,
ruby-base
,
ruby-base-container
,
ruby-text
,和
ruby-text-container
|
Chrome 不支持 No | Edge 不支持 12 — 79 |
Firefox
完整支持
38
|
IE 完整支持 7 | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
完整支持
38
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
table
,
table-cell
,
table-column
,
table-column-group
,
table-footer-group
,
table-header-group
,
table-row
,和
table-row-group
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 8 | Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
-moz-box
and
-moz-inline-box
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 64
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 64
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
-moz-deck
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 62
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 62
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
-moz-grid
,
-moz-grid-group
,和
-moz-grid-line
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 62
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 62
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
-moz-inline-grid
and
-moz-inline-stack
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 62
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 62
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
-moz-popup
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 62
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 62
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
-moz-stack
弃用
非标
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
不支持
1 — 62
注意事项
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
不支持
4 — 62
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
部分支持
不支持
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
要求使用供应商前缀或不同名称。
visibility
,
float
,
position
grid
,
flex