1. Introduction
This section is normative.
CSS takes a source document organized as a tree of elements (which can contain a mix of other elements and text nodes) and text nodes (which can contain text), and renders it onto a canvas such as your screen, a piece of paper, or an audio stream. Although any such source document can be rendered with CSS, the most commonly used type is the DOM. [DOM] (Some of these more complex tree types might have additional types of nodes, such as the comment nodes in the DOM. For the purposes of CSS, all of these additional types of nodes are ignored, as if they didn’t exist.)
To do this, it generates an intermediary structure, the box tree, which represents the formatting structure of the rendered document. Each box in the box tree represents its corresponding element (or pseudo-element) in space and/or time on the canvas, while each text sequence in the box tree likewise represents the corresponding contents of its text nodes.
To create the box tree, CSS first uses cascading and inheritance, to assign a computed value for each CSS property to each element and text node in the source tree. (See [CSS-CASCADE-3].)
Then, for each element, CSS generates zero or more boxes as specified by that element’s display property. Typically, an element generates a single box, the principal box, which represents itself and contains its contents in the box tree. However, some display values (e.g. display: list-item) generate more than one box (e.g. a principal block box and a child marker box). And some values (such as none or contents) cause the element and/or its descendants to not generate any boxes at all. Boxes are often referred to by their display type—e.g. a box generated by an element with display: block is called a “block box” or just a “block”.
A box is assigned the same styles as its generating element, unless otherwise indicated.
In general, inherited properties are assigned to the principal box,
and then inherit through the box tree
to any other boxes generated by the same element.
Non-inherited properties default to applying to the principal box,
but when the element generates multiple boxes,
are sometimes defined to apply to a different box:
for example, the border properties applied to a table element
are applied to its table grid box,
not to its principal table wrapper box.
If the value computation process alters the styles of those boxes,
and the element’s style is requested
(such as through getComputedStyle()),
the element reflects,
for each property,
the value from the box to which that property was applied.
Similarly, each contiguous sequence of sibling text nodes generates a text sequence containing their text contents, which is assigned the same styles as the generating text nodes. If the sequence contains no text, however, it does not generate a text sequence.
In constructing the box tree, boxes generated by an element are descendants of the principal box of any ancestor elements. In the general case, the direct parent box of an element’s principal box is the principal box of its nearest ancestor element that generates a box; however, there are some exceptions, such as for run-in boxes, display types (like tables) that generate multiple container boxes, and intervening anonymous boxes.
An anonymous box is a box that is not associated with any element. Anonymous boxes are generated in certain circumstances to fix up the box tree when it requires a particular nested structure that is not provided by the boxes generated from the element tree. For example, a table cell box requires a particular type of parent box (the table row box), and will generate an anonymous table row box around itself if its parent is not a table row box. (See [CSS2] § 17.2.1.) Unlike element-generated boxes, whose styles inherit strictly through the element tree, anonymous boxes (which only exist in the box tree) inherit through their box tree parentage.
In the course of layout, boxes and text sequences can be broken into multiple fragments. This happens, for example, when an inline box and/or text sequence is broken across lines, or when a block box is broken across pages or columns, in a process called fragmentation. It can also happen due to bidi reordering of text (see Applying the Bidirectional Reordering Algorithm in CSS Writing Modes) or higher-level display type box splitting, e.g. block-in-inline splitting (see CSS2§9.2) or column-spanner-in-block splitting (see CSS Multi-column Layout). A box therefore consists of one or more box fragments, and a text sequence consists of one or more text fragments. See [CSS-BREAK-3] for more information on fragmentation.
Note: Many of the CSS specs were written before this terminology was ironed out, or refer to things incorrectly, so view older specs with caution when they’re using these terms. It should be possible to infer from context which term they really mean. Please report errors in specs when you find them, so they can be corrected.
Note: Further information on the “aural” box tree and its interaction with the display property can be found in the CSS Speech Module. [CSS-SPEECH-1]
Tests
1.1. Module interactions
This module replaces and extends the definition of the display property defined in [CSS2] section 9.2.4.
None of the properties in this module apply to the ::first-line or ::first-letter pseudo-elements.
Tests
1.2. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Box Layout Modes: the display property
| Name: | display |
|---|---|
| Value: | [ <display-outside> || <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy> |
| Initial: | inline |
| Applies to: | all elements |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | a pair of keywords representing the inner and outer display types plus optional list-item flag, or a <display-internal> or <display-box> keyword; see prose in a variety of specs for computation rules |
| Canonical order: | per grammar |
| Animation type: | see § 2.9 Animating and Interpolating display |
User agents are expected to support this property on all media, including non-visual ones. The display property defines an element’s display type, which consists of the two basic qualities of how an element generates boxes:
-
the inner display type, which defines (if it is a non-replaced element) the kind of formatting context it generates, dictating how its descendant boxes are laid out. (The inner display of a replaced element is outside the scope of CSS.)
-
the outer display type, which dictates how the principal box itself participates in flow layout.
Text sequences have no display type.
Some display values have additional side-effects: such as list-item, which also generates a ::marker pseudo-element, and none, which causes the element’s entire subtree to be left out of the box tree.
Values are defined as follows:
<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-table | inline-flex | inline-grid
The following informative table summarizes the values of display:
Note: Following the precedence rules of “most backwards-compatible, then shortest”, serialization of equivalent display values uses the “Short display” column. [CSSOM]
Tests
- display-interpolation.html (live test) (source)
- display-math-on-non-mathml-elements.html (live test) (source)
- display-math-on-pseudo-elements-001.html (live test) (source)
- display-math-on-pseudo-elements-002.html (live test) (source)
- display-none-root-hit-test-crash.html (live test) (source)
- inheritance.html (live test) (source)
- display-computed.html (live test) (source)
- display-invalid.html (live test) (source)
- display-valid.html (live test) (source)
- select-4-option-optgroup-display-none.html (live test) (source)
- textarea-display.html (live test) (source)
2.1. Outer Display Roles for Flow Layout: the block, inline, and run-in keywords
The <display-outside> keywords specify the element’s outer display type, which is essentially its principal box’s role in flow layout. They are defined as follows:
- block
- The element generates a box that is block-level when placed in flow layout. [CSS2]
- inline
- The element generates a box that is inline-level when placed in flow layout. [CSS2]
- run-in
- The element generates an run-in box, which is a type of inline-level box with special behavior that attempts to merge it into a subsequent block container. See § 6 Run-In Layout for details.
Note: Outer display types do affect replaced elements.
If a <display-outside> value is specified but <display-inside> is omitted, the element’s inner display type defaults to flow.
Tests
- display-change-iframe.html (live test) (source)
- display-change-object-iframe.html (live test) (source)
- after-content-display-004.xht (visual test) (source)
- anonymous-box-generation-002.xht (visual test) (source)
- background-applies-to-011.xht (visual test) (source)
- background-attachment-applies-to-011.xht (visual test) (source)
- background-color-applies-to-011.xht (visual test) (source)
- background-image-applies-to-011.xht (visual test) (source)
- background-position-applies-to-011.xht (visual test) (source)
- background-repeat-applies-to-011.xht (visual test) (source)
- before-content-display-004.xht (visual test) (source)
- border-applies-to-011.xht (visual test) (source)
- border-bottom-applies-to-011.xht (visual test) (source)
- border-bottom-color-applies-to-011.xht (visual test) (source)
- border-bottom-style-applies-to-011.xht (visual test) (source)
- border-bottom-width-applies-to-011.xht (visual test) (source)
- border-collapse-applies-to-004.xht (visual test) (source)
- border-color-applies-to-011.xht (visual test) (source)
- border-left-applies-to-011.xht (visual test) (source)
- border-left-color-applies-to-011.xht (visual test) (source)
- border-left-style-applies-to-011.xht (visual test) (source)
- border-left-width-applies-to-011.xht (visual test) (source)
- border-right-applies-to-011.xht (visual test) (source)
- border-right-color-applies-to-011.xht (visual test) (source)
- border-right-style-applies-to-011.xht (visual test) (source)
- border-right-width-applies-to-011.xht (visual test) (source)
- border-spacing-applies-to-004.xht (visual test) (source)
- border-style-applies-to-011.xht (visual test) (source)
- border-top-applies-to-011.xht (visual test) (source)
- border-top-color-applies-to-011.xht (visual test) (source)
- border-top-style-applies-to-011.xht (visual test) (source)
- border-top-width-applies-to-011.xht (visual test) (source)
- border-width-applies-to-011.xht (visual test) (source)
- bottom-applies-to-011.xht (visual test) (source)
- caption-side-applies-to-004.xht (visual test) (source)
- clear-applies-to-011.xht (visual test) (source)
- clear-runin-001.xht (visual test) (source)
- color-applies-to-011.xht (visual test) (source)
- counter-increment-applies-to-011.xht (live test) (source)
- counter-reset-applies-to-011.xht (live test) (source)
- cursor-applies-to-011.xht (manual test) (source)
- direction-applies-to-011.xht (visual test) (source)
- display-004.xht (visual test) (source)
- empty-cells-applies-to-004.xht (visual test) (source)
- first-line-pseudo-009.xht (visual test) (source)
- float-applies-to-011.xht (visual test) (source)
- font-applies-to-004.xht (visual test) (source)
- font-family-applies-to-004.xht (visual test) (source)
- font-size-applies-to-004.xht (visual test) (source)
- font-style-applies-to-004.xht (live test) (source)
- font-variant-applies-to-004.xht (live test) (source)
- font-weight-applies-to-004.xht (live test) (source)
- height-applies-to-011.xht (live test) (source)
- left-applies-to-011.xht (visual test) (source)
- letter-spacing-applies-to-004.xht (live test) (source)
- line-height-applies-to-011.xht (visual test) (source)
- list-style-applies-to-011.xht (live test) (source)
- list-style-image-applies-to-011.xht (visual test) (source)
- list-style-position-applies-to-011.xht (visual test) (source)
- list-style-type-applies-to-011.xht (live test) (source)
- margin-applies-to-011.xht (visual test) (source)
- margin-bottom-applies-to-011.xht (visual test) (source)
- margin-left-applies-to-011.xht (visual test) (source)
- margin-right-applies-to-011.xht (visual test) (source)
- margin-top-applies-to-011.xht (visual test) (source)
- max-height-applies-to-011.xht (live test) (source)
- max-width-applies-to-011.xht (live test) (source)
- min-height-applies-to-011.xht (live test) (source)
- min-width-applies-to-011.xht (live test) (source)
- outline-applies-to-011.xht (visual test) (source)
- outline-color-applies-to-011.xht (visual test) (source)
- outline-style-applies-to-011.xht (visual test) (source)
- outline-width-applies-to-011.xht (visual test) (source)
- overflow-applies-to-011.xht (visual test) (source)
- padding-applies-to-011.xht (visual test) (source)
- padding-bottom-applies-to-011.xht (visual test) (source)
- padding-left-applies-to-011.xht (visual test) (source)
- padding-right-applies-to-011.xht (visual test) (source)
- padding-top-applies-to-011.xht (visual test) (source)
- position-applies-to-011.xht (visual test) (source)
- quotes-applies-to-011.xht (live test) (source)
- right-applies-to-011.xht (visual test) (source)
- run-in-001.xht (visual test) (source)
- run-in-002.xht (visual test) (source)
- run-in-003.xht (visual test) (source)
- run-in-004.xht (visual test) (source)
- run-in-005.xht (visual test) (source)
- run-in-006.xht (visual test) (source)
- run-in-007.xht (visual test) (source)
- run-in-008.xht (visual test) (source)
- run-in-009.xht (visual test) (source)
- run-in-010.xht (visual test) (source)
- run-in-011.xht (visual test) (source)
- run-in-012.xht (visual test) (source)
- run-in-013.xht (visual test) (source)
- run-in-abspos-between-001.xht (live test) (source)
- run-in-abspos-between-002.xht (live test) (source)
- run-in-abspos-between-003.xht (live test) (source)
- run-in-basic-001.xht (live test) (source)
- run-in-basic-002.xht (live test) (source)
- run-in-basic-003.xht (live test) (source)
- run-in-basic-004.xht (live test) (source)
- run-in-basic-005.xht (live test) (source)
- run-in-basic-006.xht (live test) (source)
- run-in-basic-007.xht (live test) (source)
- run-in-basic-008.xht (live test) (source)
- run-in-basic-009.xht (live test) (source)
- run-in-basic-010.xht (live test) (source)
- run-in-basic-011.xht (live test) (source)
- run-in-basic-012.xht (live test) (source)
- run-in-basic-013.xht (live test) (source)
- run-in-basic-014.xht (live test) (source)
- run-in-basic-015.xht (live test) (source)
- run-in-basic-016.xht (live test) (source)
- run-in-basic-017.xht (live test) (source)
- run-in-basic-018.xht (live test) (source)
- run-in-block-between-001.xht (live test) (source)
- run-in-block-between-002.xht (live test) (source)
- run-in-block-between-003.xht (live test) (source)
- run-in-breaking-001.xht (live test) (source)
- run-in-breaking-002.xht (live test) (source)
- run-in-clear-001.xht (live test) (source)
- run-in-clear-002.xht (live test) (source)
- run-in-contains-abspos-001.xht (live test) (source)
- run-in-contains-block-001.xht (live test) (source)
- run-in-contains-block-002.xht (live test) (source)
- run-in-contains-block-003.xht (live test) (source)
- run-in-contains-block-004.xht (live test) (source)
- run-in-contains-block-005.xht (live test) (source)
- run-in-contains-block-inside-inline-001.xht (live test) (source)
- run-in-contains-block-inside-inline-002.xht (live test) (source)
- run-in-contains-block-inside-inline-003.xht (live test) (source)
- run-in-contains-float-001.xht (live test) (source)
- run-in-contains-inline-001.xht (live test) (source)
- run-in-contains-inline-002.xht (live test) (source)
- run-in-contains-inline-003.xht (live test) (source)
- run-in-contains-inline-004.xht (live test) (source)
- run-in-contains-inline-005.xht (live test) (source)
- run-in-contains-inline-006.xht (live test) (source)
- run-in-contains-inline-007.xht (live test) (source)
- run-in-contains-inline-block-001.xht (live test) (source)
- run-in-contains-inline-table-001.xht (live test) (source)
- run-in-contains-relpos-block-001.xht (live test) (source)
- run-in-contains-relpos-block-002.xht (live test) (source)
- run-in-contains-relpos-block-003.xht (live test) (source)
- run-in-contains-run-in-001.xht (live test) (source)
- run-in-contains-run-in-002.xht (live test) (source)
- run-in-contains-run-in-003.xht (live test) (source)
- run-in-contains-table-001.xht (live test) (source)
- run-in-contains-table-002.xht (live test) (source)
- run-in-contains-table-003.xht (live test) (source)
- run-in-contains-table-caption-001.xht (live test) (source)
- run-in-contains-table-cell-001.xht (live test) (source)
- run-in-contains-table-column-001.xht (live test) (source)
- run-in-contains-table-column-group-001.xht (live test) (source)
- run-in-contains-table-inside-inline-001.xht (live test) (source)
- run-in-contains-table-inside-inline-002.xht (live test) (source)
- run-in-contains-table-inside-inline-003.xht (live test) (source)
- run-in-contains-table-row-001.xht (live test) (source)
- run-in-contains-table-row-group-001.xht (live test) (source)
- run-in-display-none-between-001.xht (live test) (source)
- run-in-display-none-between-002.xht (live test) (source)
- run-in-display-none-between-003.xht (live test) (source)
- run-in-fixedpos-between-001.xht (live test) (source)
- run-in-fixedpos-between-002.xht (live test) (source)
- run-in-fixedpos-between-003.xht (live test) (source)
- run-in-float-between-001.xht (live test) (source)
- run-in-float-between-002.xht (live test) (source)
- run-in-float-between-003.xht (live test) (source)
- run-in-inherit-001.xht (live test) (source)
- run-in-inheritance-001.xht (visual test) (source)
- run-in-inline-between-001.xht (live test) (source)
- run-in-inline-between-002.xht (live test) (source)
- run-in-inline-between-003.xht (live test) (source)
- run-in-inline-block-between-001.xht (live test) (source)
- run-in-inline-block-between-002.xht (live test) (source)
- run-in-inline-block-between-003.xht (live test) (source)
- run-in-inline-table-between-001.xht (live test) (source)
- run-in-inline-table-between-002.xht (live test) (source)
- run-in-inline-table-between-003.xht (live test) (source)
- run-in-linebox-001.xht (visual test) (source)
- run-in-linebox-002.xht (visual test) (source)
- run-in-listitem-between-001.xht (live test) (source)
- run-in-listitem-between-002.xht (live test) (source)
- run-in-listitem-between-003.xht (live test) (source)
- run-in-relpos-between-001.xht (live test) (source)
- run-in-relpos-between-002.xht (live test) (source)
- run-in-relpos-between-003.xht (live test) (source)
- run-in-replaced-001.xht (live test) (source)
- run-in-restyle-001.xht (live test) (source)
- run-in-restyle-002.xht (live test) (source)
- run-in-restyle-003.xht (live test) (source)
- run-in-run-in-between-001.xht (live test) (source)
- run-in-run-in-between-002.xht (live test) (source)
- run-in-run-in-between-003.xht (live test) (source)
- run-in-run-in-between-004.xht (live test) (source)
- run-in-run-in-between-005.xht (live test) (source)
- run-in-run-in-between-006.xht (live test) (source)
- run-in-run-in-between-007.xht (live test) (source)
- run-in-run-in-between-008.xht (live test) (source)
- run-in-table-between-001.xht (live test) (source)
- run-in-table-between-002.xht (live test) (source)
- run-in-table-between-003.xht (live test) (source)
- run-in-table-cell-between-001.xht (live test) (source)
- run-in-table-cell-between-002.xht (live test) (source)
- run-in-table-cell-between-003.xht (live test) (source)
- run-in-table-row-between-001.xht (live test) (source)
- run-in-table-row-between-002.xht (live test) (source)
- run-in-table-row-between-003.xht (live test) (source)
- run-in-text-between-001.xht (live test) (source)
- run-in-text-between-002.xht (live test) (source)
- run-in-text-between-003.xht (live test) (source)
- run-in-text-between-004.xht (live test) (source)
- run-in-text-between-005.xht (live test) (source)
- table-anonymous-block-001.xht (visual test) (source)
- table-layout-applies-to-004.xht (visual test) (source)
- text-align-applies-to-004.xht (visual test) (source)
- text-decoration-applies-to-004.xht (live test) (source)
- text-indent-applies-to-004.xht (visual test) (source)
- text-transform-applies-to-004.xht (live test) (source)
- top-applies-to-011.xht (visual test) (source)
- unicode-bidi-applies-to-011.xht (visual test) (source)
- vertical-align-applies-to-011.xht (visual test) (source)
- visibility-applies-to-011.xht (visual test) (source)
- white-space-applies-to-004.xht (visual test) (source)
- width-applies-to-011.xht (live test) (source)
- word-spacing-applies-to-004.xht (visual test) (source)
- z-index-applies-to-011.xht (visual test) (source)
2.2. Inner Display Layout Models: the flow, flow-root, table, flex, grid, and ruby keywords
The <display-inside> 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:
- 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. See CSS2.1 Chapter 9. [CSS2] A block container that establishes a new block formatting context is considered to have a used inner display type of flow-root.
- flow-root
- The element generates a block container box, and lays out its contents using flow layout. It always establishes a new block formatting context for its contents. [CSS2]
- table
- The element generates a principal table wrapper box that establishes a block formatting context, and which contains an additionally-generated table grid box that establishes a table formatting context. [CSS2]
- flex
- The element generates a principal flex container box and establishes a flex formatting context. [CSS-FLEXBOX-1]
- grid
-
The element generates a principal grid container box,
and establishes a grid formatting context. [CSS-GRID-1]
(Grids using subgrid might not generate a new grid formatting context; see [CSS-GRID-2] for details.)
- ruby
- The element generates a ruby container box and establishes a ruby formatting context in addition to integrating its base-level contents into its parent formatting context (if it is inline) or generating a wrapper box of the appropriate outer display type (if it is not). [CSS-RUBY-1]
If a <display-inside> value is specified but <display-outside> is omitted, the element’s outer display type defaults to block—except for ruby, which defaults to inline.
Tests
2.3. Generating Marker Boxes: the list-item keyword
The list-item keyword
causes the element to generate a ::marker pseudo-element [CSS-PSEUDO-4]
with the content specified by its list-style properties
(CSS 2.1§12.5 Lists) [CSS2]
together with a principal box of the specified type for its own contents.
If no inner display type value is specified, the principal box’s inner display type defaults to flow. If no outer display type value is specified, the principal box’s outer display type defaults to block.
Note: In this level, as restricted in the grammar, list-items are limited to the Flow Layout display types (block/inline/run-in with flow/flow-root inner types). This restriction may be relaxed in a future level of this module.
Tests
2.4. Layout-Internal Display Types: the table-* and ruby-* keywords
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 “layout-internal” display values, which only have me