这篇翻译不完整。请帮忙从英语翻译这篇文章

display CSS属性指定用于元素的呈现框的类型。在 HTML 中,默认的 display 属性取决于 HTML 规范所描述的行为或浏览器/用户的默认样式表。在 XML中,其默认值为 inline

除了多种不同的生成的元素的盒类型,值 none 可以关闭一个元素的显示;当你使用 none 所有的后代元素他们的显示也会被关闭。文档渲染的过程中就好像在文档树中这个元素不存在一样。

/* <display-outside> values */
display: block;
display: inline;
display: run-in;

/* <display-inside> values */
display: flow;
display: flow-root;
display: table;
display: flex;
display: grid;
display: ruby;
display: subgrid;

/* <display-outside> plus <display-inside> values */
display: block flow;
display: inline table;
display: flex run-in;

/* <display-listitem> values */
display: list-item;
display: list-item block;
display: list-item inline;
display: list-item flow;
display: list-item flow-root;
display: list-item block flow;
display: list-item block flow-root;
display: flow list-item block;

/* <display-internal> values */
display: table-row-group;
display: table-header-group;
display: table-footer-group;
display: table-row;
display: table-cell;
display: table-column-group;
display: table-column;
display: table-caption;
display: ruby-base;
display: ruby-text;
display: ruby-base-container;
display: ruby-text-container;

/* <display-box> values */
display: contents;
display: none;

/* <display-legacy> values */
display: inline-block;
display: inline-table;
display: inline-flex;
display: inline-grid;

/* Global values */
display: inherit;
display: initial;
display: unset;

初始值inline
适用元素all elements
是否是继承属性
适用媒体all
计算值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.
Animation typediscrete
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

display 属性使用关键属性值指定。 关键属性值被分为六类:

目前最好使用单个关键词指定 display ; 虽然最新规范允许您组合关键词,但浏览器并没有很好地支持。

<display-outside>
These keywords specify the element’s outer display type, which is essentially its role in flow layout. They are defined as follows:
Value Description
block The element generates a block element box.
inline The element generates one or more inline element boxes.
run-in The element generates a run-in box. Run-in elements act like inlines or blocks, depending on the surrounding elements. That is:
  • If the run-in box contains a block box, same as block.
  • If a block box follows the run-in box, the run-in box becomes the first inline box of the block box.
  • If an inline box follows, the run-in box becomes a block box.
<display-inside>
These keywords specify the element’s inner display type, which defines the type of formatting context that lays out its contents (assuming it is a non-replaced element). They are defined as follows:
Value Description
flow The element lays out its contents using flow layout (block-and-inline layout).

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, or overflow) and whether it is itself participating in a block or inline formatting context, it either establishes a new block formatting context for its contents or integrates its contents into its parent formatting context.

flow-root The element generates a block element box that establishes a new block formatting context.
table These elements behave like <table> HTML elements. It defines a block-level box.
flex The element behaves like a block element and lays out its content according to the flexbox model.
grid The element behaves like a block element and lays out its content according to the grid model.
subgrid If the parent element has display:grid, the element itself and its content are laid out according to the grid model.
ruby The element behaves like an inline element and lays out its content according to the ruby formatting model. It behaves like the corresponding <ruby> HTML elements.
<display-listitem>

The element generates a block box for the content and a separate list-item inline box.

Value Description
list-item  

If no <display-inside> value is specified, the principal box’s inner display type defaults to flow. If no <display-outside> value is specified, the principal box’s outer display type defaults to block.

<display-internal>

Some layout models, such as 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.

Unless otherwise specified, both the inner display type and the outer display type of elements using these display values are set to the given keyword.

Value Description
table-row-group These elements behave like <tbody> HTML elements
table-header-group These elements behave like <thead> HTML elements.
table-footer-group These elements behave like <tfoot> HTML elements.
table-row These elements behave like <tr> HTML elements.
table-cell These elements behave like <td> HTML elements.
table-column-group These elements behave like <colgroup> HTML elements.
table-column These elements behave like <col> HTML elements.
table-caption These elements behave like <caption> HTML elements.
ruby-base These elements behave like <rb> elements.
ruby-text These elements behave like <rt> elements.
ruby-base-container These elements behave like <rbc> elements generated as anonymous boxes.
ruby-text-container These elements behave like <rtc> elements.
<display-box>
These values define whether an element generates display boxes at all.
Value Description
contents These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes. Please note that CSS Display Level 3 spec defines how the 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 for more details.
This will also remove the element from the accessibility tree — screen readers will not look at what's inside.
none

Turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist). All descendant elements also have their display turned off.

To have an element take up the space that it would normally take, but without actually rendering anything, use the visibility property instead.

<display-legacy>
CSS 2 used a single-keyword syntax for the display property, requiring separate keywords for block-level and inline-level variants of the same layout mode. They are defined as follows:
Value Description
inline-block

The element generates a block element box that will be flowed with surrounding content as if it were a single inline box (behaving much like a replaced element would)

It is equivalent to inline flow-root.

inline-table

The inline-table value does not have a direct mapping in HTML. It behaves like a <table> HTML element, but as an inline box, rather than a block-level box. Inside the table box is a block-level context.

It is equivalent to inline table.

inline-flex

The element behaves like an inline element and lays out its content according to the flexbox model.

It is equivalent to inline flex.

inline-grid The element behaves like an inline element and lays out its content according to the grid model.

XUL values

非标准
该特性是非标准的,请尽量不要在生产环境中使用它!

Firefox supports the following -moz- prefixed XUL display values:

Value Description
-moz-box XUL box, mostly equivalent to flex
-moz-inline-box XUL inline box, mostly equivalent to inline-flex
-moz-grid XUL grid
-moz-inline-grid XUL inline grid
-moz-grid-group XUL grid group
-moz-grid-line XUL grid line
-moz-stack XUL stack
-moz-inline-stack XUL inline stack
-moz-deck XUL deck
-moz-popup XUL popup

All XUL display values, with the exception of -moz-box and -moz-inline-box, have been removed in bug 1288572. See this Firefox Site Compatibility post for more details.

The -moz-box and -moz-inline-box values will be removed later in bug 879275.

Formal syntax

[ <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

示例

隐藏元素

HTML

<p>Visible text</p>
<p class="secret">Invisible text</p>

CSS

p.secret {
  display: none;
}

结果

Accessibility concerns

display: none;

Using a display value of none or content 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.

Tables

Changing the display value of a table element to blockgrid, or flex will alter its representation in the accessibility tree. This will cause the table to no longer be announced properly by screen reading technology. 

规范

Specification Status Comment
CSS Display Module Level 3
display
Working Draft Added run-in, flow, flow-root, contents, and multi-keyword values.
CSS Ruby Layout Module Level 1
display
Working Draft Added ruby, ruby-base, ruby-text, ruby-base-container, and ruby-text-container.
CSS Grid Layout
display
Candidate Recommendation Added the grid box model values.
CSS Flexible Box Layout Module
display
Candidate Recommendation Added the flexible box model values.
CSS Level 2 (Revision 1)
display
Recommendation Added the table model values and inline-block.
CSS Level 1
display
Recommendation Initial definition. Basic values: none, block, inline, and list-item.

浏览器兼容

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 7Safari Full support 1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Multi-keyword values
Experimental
Chrome ? Edge ? Firefox No support NoIE No support NoOpera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android No support NoOpera Android ? Safari iOS ? Samsung Internet Android ?
list-itemChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 7Safari Full support 1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
inline-blockChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 8
Full support 8
Partial support 6
Notes
Notes Until Internet Explorer 8, inline-block is only for natural inline elements.
Opera Full support 7Safari Full support 1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
inline-tableChrome Full support 1Edge Full support 12Firefox Full support 3IE Full support 8Opera Full support 7Safari Full support 1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
table, table-cell, table-column, table-column-group, table-footer-group, table-header-group, table-row, and table-row-groupChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 8Opera Full support 7Safari Full support 1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
flexChrome Full support 29
Full support 29
Full support 21
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Edge Full support 12Firefox Full support 20
Notes
Full support 20
Notes
Notes Firefox 28 added multi-line flexbox support.
No support 18 — 28
Disabled
Disabled From version 18 until version 28 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 11
Full support 11
Full support 8
Alternate Name
Alternate Name Uses the non-standard name: -ms-flexbox
Opera Full support 16
Full support 16
Full support 15
Prefixed
Prefixed Requires the vendor prefix: -webkit-
No support 12.1 — 15
Safari Full support 9
Full support 9
Full support 6.1
Prefixed
Prefixed Requires the vendor prefix: -webkit-
WebView Android Full support 4.4
Full support 4.4
Full support Yes
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Chrome Android Full support 29
Full support 29
Full support 25
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Edge Mobile Full support 12Firefox Android Full support 20
Notes
Full support 20
Notes
Notes Firefox 28 added multi-line flexbox support.
No support 18 — 28
Disabled
Disabled From version 18 until version 28 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 16
Full support 16
Full support 14
Prefixed
Prefixed Requires the vendor prefix: -webkit-
No support 12.1 — 14
Safari iOS Full support 9
Full support 9
Full support 7.1
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Samsung Internet Android Full support Yes
inline-flexChrome Full support 29
Full support 29
Full support 21
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Edge Full support 12Firefox Full support 20
Notes
Full support 20
Notes
Notes Firefox 28 added multi-line flexbox support.
No support 18 — ?
Disabled
Disabled From version 18: this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 11
Full support 11
Full support 8
Alternate Name
Alternate Name Uses the non-standard name: -ms-inline-flexbox
Opera Full support 16
Full support 16
Full support 15
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Safari Full support 9
Full support 9
Full support 6.1
Prefixed
Prefixed Requires the vendor prefix: -webkit-
WebView Android Full support 4.4
Full support 4.4
Full support Yes
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Chrome Android Full support 29
Full support 29
Full support 25
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Edge Mobile Full support 12Firefox Android Full support 20
Notes
Full support 20
Notes
Notes Firefox 28 added multi-line flexbox support.
No support 18 — ?
Disabled
Disabled From version 18: this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 16
Full support 16
Full support 14
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Safari iOS Full support 9
Full support 9
Full support 6.1
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Samsung Internet Android Full support Yes
grid
Experimental
Chrome Full support 57Edge Full support 16
Full support 16
Full support 12
Prefixed
Prefixed Requires the vendor prefix: -ms-
Firefox Full support 52IE Partial support 10
Prefixed Notes
Partial support 10
Prefixed Notes
Prefixed Requires the vendor prefix: -ms-
Notes Internet Explorer implements an older version of the specification.
Opera Full support 44Safari Full support YesWebView Android Full support 57Chrome Android Full support 57Edge Mobile Full support 16
Full support 16
Full support 12
Prefixed
Prefixed Requires the vendor prefix: -ms-
Firefox Android Full support 52Opera Android Full support 44Safari iOS Full support YesSamsung Internet Android Full support 7.0
inline-grid
Experimental
Chrome Full support 57Edge Full support 16
Full support 16
Full support 12
Prefixed
Prefixed Requires the vendor prefix: -ms-
Firefox Full support 52IE Partial support 10
Prefixed Notes
Partial support 10
Prefixed Notes
Prefixed Requires the vendor prefix: -ms-
Notes Internet Explorer implements an older version of the specification.
Opera Full support 44Safari ? WebView Android Full support 57Chrome Android Full support 57Edge Mobile Full support 16
Full support 16
Full support 12
Prefixed
Prefixed Requires the vendor prefix: -ms-
Firefox Android Full support 52Opera Android Full support 44Safari iOS ? Samsung Internet Android Full support 7.0
subgrid
Deprecated
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
ruby, ruby-base, ruby-base-container, ruby-text, and ruby-text-container
Experimental
Chrome ? Edge Full support 12Firefox Full support 38
Full support 38
No support 34 — 38
Disabled
Disabled From version 34 until version 38 (exclusive): this feature is behind the layout.css.ruby.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile Full support 12Firefox Android Full support 38
Full support 38
No support 34 — 38
Disabled
Disabled From version 34 until version 38 (exclusive): this feature is behind the layout.css.ruby.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?
run-in
Experimental
Chrome No support 1 — 32
Notes
No support 1 — 32
Notes
Notes Before Chrome 4, run-in was not supported before inline elements.
Edge No support NoFirefox No support NoIE Full support 8Opera No support 7 — 15Safari No support 1 — 8
Notes
No support 1 — 8
Notes
Notes Before Safari 5, run-in was not supported before inline elements.
WebView Android No support ? — 4.4.3
Notes
No support ? — 4.4.3
Notes
Notes Before version 4, run-in was not supported before inline elements.
Chrome Android No support ? — 32
Notes
No support ? — 32
Notes
Notes Before Chrome 4, run-in was not supported before inline elements.
Edge Mobile No support NoFirefox Android No support NoOpera Android ? Safari iOS No support ? — 8
Notes
No support ? — 8
Notes
Notes Before Safari 5, run-in was not supported before inline elements.
Samsung Internet Android ?
flow-root
Experimental
Chrome Full support 58Edge ? Firefox Full support 53IE ? Opera Full support 45Safari ? WebView Android Full support 58Chrome Android Full support 58Edge Mobile ? Firefox Android Full support 53Opera Android Full support 45Safari iOS ? Samsung Internet Android Full support 7.0
contents
Experimental
Chrome Full support 65
Full support 65
No support 58 — 65
Disabled
Disabled From version 58 until version 65 (exclusive): this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge No support NoFirefox Full support 37
Full support 37
No support 36 — 53
Disabled
Disabled From version 36 until version 53 (exclusive): this feature is behind the layout.css.display-contents.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 52Safari Full support 11.1WebView Android Full support 65Chrome Android Full support 65Edge Mobile No support NoFirefox Android Full support 57Opera Android Full support 52Safari iOS Full support 11.1Samsung Internet Android No support No
-moz-box and -moz-inline-box
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox Full support Yes
Notes
Full support Yes
Notes
Notes Scheduled for removal (See bug 879275).
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes Scheduled for removal (See bug 879275).
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
-moz-grid, -moz-inline-grid, -moz-grid-group and -moz-grid-line
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support ? — 62IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support ? — 62Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
-moz-stack and -moz-inline-stack
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support ? — 62IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support ? — 62Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
-moz-deck
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support ? — 62IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support ? — 62Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
-moz-popup
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support ? — 62IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support ? — 62Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

参见

文档标签和贡献者

标签: 
最后编辑者: zhangchen,