display - CSS(层叠样式表) 编辑

display 属性可以设置元素的内部和外部显示类型 display types。元素的外部显示类型 outer display types 将决定该元素在流式布局中的表现(块级或内联元素);元素的内部显示类型 inner display types 可以控制其子元素的布局(例如:flow layoutgridflex)。

CSS 规范(特指 CSS Level 1/2/3 规范)中查阅 display 属性的所有取值时需要注意:个别取值的详细信息记录在独立的规范中。 例如,display: flex 的详细信息在 CSS Flexible Box Model 规范中记录。可以在本文档末尾的表格中查看所有有关的规范。

语法

display 属性使用关键字取值来指定,关键字取值被分为六类:

.container {
  display:  [ <display-outside> | <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy> ;
}

Outside

<display-outside>
这些关键字指定了元素的外部显示类型,实际上就是其在流式布局中的角色(即在流式布局中的表现)。

Inside

<display-inside>
这些关键字指定了元素的内部显示类型,它们定义了该元素内部内容的布局方式(假定该元素为非替换元素 non-replaced element)。

Valid <display-inside> values:

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 (BFC) 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, defining where the formatting root lies.
table
These elements behave like HTML <table> 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.
ruby
The element behaves like an inline element and lays out its content according to the ruby formatting model. It behaves like the corresponding HTML <ruby> elements.

Note: 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.

List Item

<display-listitem>
将这个元素的外部显示类型变为 block 盒,并将内部显示类型变为多个 list-item inline 盒。

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.

Note: 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.

Internal

<display-internal>
有些布局模型(如 table 和 ruby)有着复杂的内部结构,因此它们的子元素可能扮演着不同的角色。这一类关键字就是用来定义这些“内部”显示类型,并且只有在这些特定的布局模型中才有意义。

Valid <display-internal> values:

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> HTML elements.
ruby-text
These elements behave like <rt> HTML elements.
ruby-base-container
These elements behave like <rbc> HTML elements generated as anonymous boxes.
ruby-text-container
These elements behave like <rtc> HTML elements.

Box

<display-box>
这些值决定元素是否使用盒模型。

Valid <display-box> values:

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 the 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.

Due to a bug in browsers this will currently remove the element from the accessibility tree — screen readers will not look at what's inside. See the Accessibility concerns section below for more details.
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.

Legacy

<display-legacy>
CSS 2 对于 display 属性使用单关键字语法,对于相同布局模式的 block 级和 inline 级变体需要使用单独的关键字。

Valid <display-legacy> values:

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 an HTML <table> 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.

It is equivalent to inline grid.

是否该继续使用 Legacy 取值?

CSS Level 3 规范详细说明了 display 属性的两类取值——显式地指定了外部和内部显示属性的规范——但是还没有被浏览器广泛支持。

<display-legacy> 方法允许使用单个关键字达到相同效果,开发者应该在双关键字取值被广泛支持之前采用这个方法。举例来说,你可以这样指定 inline flex 容器:

.container {
    display: inline-flex;
}

也可以用两个关键字来指定。

.container {
    display: inline flex;
}

有关规范变更的更多信息,查阅文章 Adapting to the new two-value syntax of display

全局设置

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

指南和示例

上面 语法 部分提供了 display 取值的多个示例。下面的资料将详细介绍 display 的各个取值。

CSS Flow Layout (display: block, display: inline)

display: flex

display: grid

除此之外,你可以在 MDN 上找到布局模型的详细解释:

可访问性相关

display: none;

将 display 设置为 none 会将元素从 可访问性树 accessibility tree 中移除。这会导致该元素及其所有子代元素不再被屏幕阅读技术 screen reading technology 访问。

如果你只是想从视觉上隐藏这个元素,对可访问性更加友好的做法是使用 属性组合 来将其从屏幕上隐藏,但仍可以被屏幕阅读器 screen readers  等辅助技术解析。

display: contents;

当前大多数浏览器对 display: contents; 的实现是:将设置了该值的元素从 可访问性树 accessibility tree 中移除,但保留其子代元素。这会导致该元素自身不再被屏幕阅读技术 screen reading technology  访问。这在 CSS 规范 中被视为不正确的行为。

Tables

将 <table> 元素的 display 值修改为 block, grid, 或 flex 会修改其在 可访问性树 accessibility tree 中的表现,这会使得 table 元素不能被屏幕阅读技术正常处理。

规范

规范状态备注
CSS Display Module Level 3
display
Candidate Recommendation新增 run-in, flow, flow-root, contents, 以及多关键字取值。
CSS Ruby Layout Module Level 1
display
Working Draft新增 ruby, ruby-base, ruby-text, ruby-base-container, 和 ruby-text-container
CSS Grid Layout
display
Candidate Recommendation新增 grid 盒模型的值。
CSS Flexible Box Layout Module
display
Candidate Recommendation新增 flexible 盒模型的值。
CSS Level 2 (Revision 1)
display
Recommendation新增 table 模型的值和 inline-block.
CSS Level 1
display
Recommendation初始定义.,基础值: none, block, inline, 以及 list-item.

浏览器兼容性

BCD tables only load in the browser

此页面上的兼容性表是根据结构化数据生成的。如果您想为数据做出贡献,请查看
https://github.com/mdn/browser-compat-data并向我们发送拉取请求。

参见

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:209 次

字数:30420

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文