CSS Display Module Level 3
Status: WD
Date: 2014-09-11
ED: http://dev.w3.org/csswg/css-display/
Shortname: css-display
Group: csswg
Level: 3
TR: http://www.w3.org/TR/css-display-3/
Previous Version: http://www.w3.org/TR/2014/WD-css-display-3-20140220/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
Editor: fantasai, Invited Expert, http://fantasai.inkedblade.net/contact
Abstract: This module describes how the CSS formatting box tree is generated from the document element tree and defines the 'display' and 'box-suppress' properties that control it.
Ignored Terms: display-model, display-role, table row group box, ruby container
Link Defaults: css-lists-3 (property) counter-increment
Introduction
This section is not normative.
The 'display' property, introduced in CSS 2.1,
defines what kind of boxes an element generates
(and whether it generates boxes at all),
and how it lays out its contents.
These concepts are actually rather independent,
though they're conflated by the 'display' property.
This causes some pain when a property value intended to affect one aspect
(such as setting an element to ''display:none'' to suppress box generation)
affects another aspect
(such as losing the memory of what it was before ''display:none'',
so that it can be set back to that value later).
This specification subsumes the CSS 2.1 definition of the 'display' property,
and redefines it to be a shorthand property for a small family of longhands,
each controlling an independent aspect of an element's "display".
Module interactions
This specification transforms the 'display' property into a shorthand property,
and defines several longhand properties that it expands into or effects.
This module replaces and extends the definition of the 'display' property defined in [[!CSS21]] section 9.2.4.
None of the properties in this module apply to the ::first-line
or ::first-letter
pseudo-elements.
Values
This specification follows the
CSS property
definition conventions from [[!CSS21]]. Value types not defined in
this specification are defined in CSS Level 2 Revision 1 [[!CSS21]].
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 it has not been repeated explicitly.
Controlling Layout Modes
The 'display' shorthand and its associated family of properties control the layout mode of elements
(how the element determines the sizes and positions of itself and its descendants),
and what boxes they and their descendants generate.
Telling Contents How to Lay Out: the 'display-inside' property
Name: display-inside
Value: auto | block | table | flex | grid | ruby
Initial: auto
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: a keyword
Media: all
The 'display-inside' property specifies the inner display type
of the box generated by the element,
dictating how its contents lay out
inside the box.
- auto
-
If the element's computed 'display-outside' value is ''inline-level'',
the element is an inline element,
and lays out its contents using inline layout. [[!CSS21]]
If the element's computed 'display-outside' value is an layout-specific internal type,
this elements acts as normal for its given 'display-outside' value.
Otherwise, this value computes to ''display-inside/block''.
- block
-
The element lays out its contents using block layout. [[!CSS21]]
- table
-
The element lays out its contents using table layout. [[!CSS21]]
- flex
-
The element lays out its contents using flex layout. [[!CSS3-FLEXBOX]]
- grid
-
The element lays out its contents using grid layout. [[!CSS3-GRID-LAYOUT]]
- ruby
-
The element lays out its contents using ruby layout. [[!CSS3RUBY]]
Interacting with Ancestors and Siblings: the 'display-outside' property
Name: display-outside
Value: block-level | inline-level | run-in | contents | none | 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
Initial: inline-level
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: as specified
Media: all
The 'display-outside' property specifies the outer display type
of the box generated by the element,
dictating how the element participates in its parent formatting context.
- block-level
-
The element generates a block-level box,
and participates in a block formatting context.
Other formatting contexts,
such as flex formatting contexts,
may also work with block-level elements. [[!CSS21]]
- inline-level
-
The element generates an inline-level box,
and participates in an inline formatting context. [[!CSS21]]
- run-in
-
The element generates a run-in box.
Run-in elements act like inlines or blocks,
depending on the surrounding elements.
See [[#run-in]] for details.
- contents
-
The element itself does not generate any boxes,
but its children and pseudo-elements still generate boxes as normal.
For the purposes of box generation and layout,
the element must be treated as if it had been replaced with its children and pseudo-elements in the document tree.
Issue: ''display-outside/contents'' currently only has an effect on box generation and layout.
Other things that care about the document tree are unaffected, like counter scopes.
Is this what we want?
- none
-
The element generates no boxes,
and does not participate in any formatting context.
Note: This value exists for legacy reasons,
and interacts with the separate 'box-suppress' property.
Advisement: It is recommended that 'box-suppress' be used instead of ''display: none'',
so that the element's display type is automatically preserved
for when it's no longer suppressed.
- table-row-group, table-header-group, table-footer-group, table-row, table-cell, table-column-group, table-column, table-caption
-
The element is an internal table element,
and participates in a table layout context. [[!CSS21]]
''display-outside/table-cell'' and ''display-outside/table-caption'' are layout-specific leaf types;
the rest are layout-specific internal types.
- ruby-base, ruby-text, ruby-base-container, ruby-text-container
-
The element is an internal ruby element,
and participates in a ruby layout context. [[!CSS3RUBY]]
''ruby-base'' and ''ruby-text'' are layout-specific leaf types;
''ruby-base-container'' and ''ruby-text-container'' are layout-specific internal types.
Some values of 'display-outside' are specialized for particular formatting contexts,
and don't have meaning outside of those specific contexts:
- layout-specific internal types
-
These display types require their parent and children to be of particular display types.
For example, a ''table-row'' box requires its parent to be a table row group box
and its children to be ''display-outside/table-cell'' boxes.
- layout-specific leaf types
-
These display types require their parent to be of a particular display type,
but can accept any 'display-inside' value.
For example, a ''display-outside/table-caption'' box must have a ''display-inside/table'' parent,
but can establish any kind of formatting context for its children.
Boxes with layout-specific types generate wrapper boxes around themselves
when placed in an incompatible parent,
as defined by their respective specifications.
Creating List Markers: the 'display-list' property
Name: display-list
Value: none | list-item
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: as specified
Media: all
- list-item
-
The element generates a
::marker
pseudo-element
and is considered a list item.
The 'display' shorthand property
Name: display
Value: [ <<'display-inside'>> || <<'display-outside'>> || <<'display-list'>> ] | inline | inline-list-item | inline-table | inline-flex | inline-grid
Initial: see individual properties
Applies to: see individual properties
Inherited: see individual properties
Computed value: see individual properties
Animatable: see individual properties
Media: see individual properties
The 'display' shorthand property sets all the display type properties in one declaration:
'display-outside', 'display-inside', and 'display-list'.
If only one keyword is specified
or only one keyword is specified together with ''display-list/list-item'',
the shorthand expands as specified below:
'display'
| 'display-outside'
| 'display-inside'
|
inline
| ''inline-level''
| ''auto''
|
block
| ''block-level''
| ''display-inside/block''
|
inline-block
| ''inline-level''
| ''display-inside/block''
|
list-item *
| ''block-level''
| ''display-inside/block''
|
inline-list-item *
| ''inline-level''
| ''auto''
|
table
| ''block-level''
| ''display-inside/table''
|
inline-table
| ''inline-level''
| ''display-inside/table''
|
table-caption
| ''display-outside/table-caption''
| ''display-inside/block''
|
table-cell
| ''display-outside/table-cell''
| ''display-inside/block''
|
flex
| ''block-level''
| ''display-inside/flex''
|
inline-flex
| ''inline-level''
| ''display-inside/flex''
|
grid
| ''block-level''
| ''display-inside/grid''
|
inline-grid
| ''inline-level''
| ''display-inside/grid''
|
ruby
| ''inline-level''
| ''display-inside/ruby''
|
none
| ''display-outside/none''
| ''display-inside/block''
|
contents
| ''display-outside/contents''
| ''display-inside/block''
|
all other <<'display-outside'>>
| <<'display-outside'>>
| ''auto''
|
* For ''display/list-item'' and ''display/inline-list-item'', additionally set 'display-list' to ''display-list/list-item''.
Some layout effects require blockification
or inlinification of the box type,
causing the box’s 'display-outside' property, if it is not ''display-outside/none'' or ''display-outside/contents'',
to compute to ''block-level'' or ''inline-level'' (respectively).
Some examples of this include:
- Absolute positioning or floating an element blockifies the box’s display type. [[CSS21]]
- Containment in a ruby container inlinifies the box’s display type, as described in [[CSS3RUBY]].
- A parent with a ''display-inside/grid'' or ''display-inside/flex'' 'display-inside' value blockifies the box’s display type. [[CSS3-GRID-LAYOUT]] [[CSS3-FLEXBOX]]
When a box is inlinified,
it recursively inlinifies all of its in-flow children
unless it itself establishes a new formatting context,
so that no block-level descendants
break up the inline formatting context in which it participates.
The root element’s display type is always blockified.
Additionally, a 'display-outside' of ''display-outside/contents'' computes to ''block-level'' on the root element.
Controlling box generation: the 'box-suppress' property
Name: box-suppress
Value: show | discard | hide
Initial: show
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: see prose
Media: all
If the computed value of 'display-outside' is ''display-outside/none'',
the computed value of 'box-suppress' is ''discard''.
Otherwise, the computed value is the specified value.
- show
-
The element generates boxes as normal,
per its 'display-*' properties.
- discard
-
The element generates no boxes at all.
- hide
-
The element generates boxes as normal,
but those boxes do not participate in layout in any way,
and must not be displayed.
For the purpose of any layout-related information,
such as querying for the computed value of the element's 'width' property,
it must be treated as if it did not generate any boxes.
Properties that rely on boxes but do not rely on layout,
such as animations, 'counter-increment', etc.,
must work as normal on this element and its descendants.
Issue: This needs more clarity about what "layout-related" and "participates in layout" means.
Does the box still generate anonymous boxes, etc.?
Issue: How does this affect speech? Is that "layout"?
Issue: We welcome better naming suggestions on this property.
Run-In Layout
A run-in box is a box that merges into a block that comes after it,
inserting itself at the beginning of that block’s inline-level content.
This is useful for formatting compact headlines, definitions, and other similar things,
where the appropriate DOM structure is to have a headline preceding the following prose,
but the desired display is an inline headline laying out with the text.
For example, dictionary definitions are often formatted so that the word is inline with the definition:
<dl class='dict'>
<dt>dictionary
<dd>a book that lists the words of a language in alphabetical
order and gives their meaning, or that gives the equivalent
words in a different language.
<dt>glossary
<dd>an alphabetical list of terms or words found in or relating
to a specific subject, text, or dialect, with explanations; a
brief dictionary.
</dl>
<style>
.dict > dt {
display: run-in;
}
.dict > dt::after {
content: ": "
}
</style>
Which is formatted as:
dictionary: a book that lists the words of a language
in alphabetical order and explains their meaning.
glossary: an alphabetical list of terms or words found
in or relating to a specific subject, text, or dialect,
with explanations; a brief dictionary.
A run-in box behaves exactly as an inline-level box, except:
-
If a run-in sequence is immediately followed by a block box
that does not establish a new formatting context,
it is inserted as direct children of the block box
after its ''::marker'' pseudo-element's boxes (if any),
but preceding any other boxes generates by the contents of the block
(including the box generated by the ''::before'' pseudo-element, if any).
The reparented content is then formatted as if originally parented there.
Note that only layout is affected, not inheritance,
because property inheritance for non-anonymous boxes is based only on the element tree.
-
Otherwise,
an anonymous block box is generated around the run-in sequence
and all immediately following inline-level content
(up to, but not including, the next run-in sequence, if any).
-
A run-in box with ''display-inside: auto'' inlinifies its contents.
A run-in sequence is a maximal sequence of consecutive sibling run-in boxes
and intervening white space and/or out-of-flow boxes.
Issue: Should out-of-flow elements get reparented, left behind, or break apart the sequence?
See thread.
Note: This run-in model is slightly different from the one proposed in earlier revisions of [[!CSS21]].
Glossary
The following terms are defined here for convenience:
- inline-level
-
Content that participates in inline layout.
Specifically, inline-level boxes and text.
- block-level
-
Content that participates in block layout.
Specifically, block-level boxes.
- inline
-
A non-replaced inline-level box whose 'display-inside' is ''auto''.
The contents of an inline box participate in the same inline formatting context as the inline box itself.
- atomic inline
-
An inline-level box that is replaced or that establishes a new formatting context.
(An inline-level box whose 'display-inside' is not ''auto'' establishes a new formatting context of the specified type.)
- block container
-
A box whose contents participate in a block formatting context,
i.e. one whose computed 'display-inside' value is ''display-inside/block''.
- block box
-
A block-level box that is a block container.
- block
-
Used as a shorthand for block box, block-level box, or block container box,
where unambiguous.
- containing block
-
A rectangle that forms the basis of sizing and positioning
for the boxes associated with it
(usually the children of the box that generated it).
Notably, a containing block is not a box
(it is a rectangle),
however it is often derived from the dimensions of a box.
If properties of a containing block are referenced,
they reference the values on the box that generated the containing block.
(For the initial containing block, the values are taken from the root element.)
See [[CSS21]] Section 9.1.2
and Section 10.1 for details.
- initial containing block
-
The containing block of the root element.
See CSS2.1§10.1 for continuous media;
and [[!CSS3PAGE]] for paged media.
- formatting context
-
The thing that makes CSS do the layouts.
Issue: wordsmith this.
Block and inline formatting contexts are defined in CSS 2.1 Section 9.4.
See [[!CSS21]] Chapter 9 for a fuller definition of these terms.
Acknowledgments
We would like to thank the many people who have attempted to separate out the disparate details of box generation over the years,
most particularly Bert Bos,
whose last attempt with 'display-model' and 'display-role' didn't get anywhere,
but primed us for the current spec,
and Anton Prowse,
whose relentless assault on CSS2.1 Chapter 9 forced some order out of the chaos.
We would also like to thank the many JavaScript libraries such as jQuery
which have hacked around the "what 'display' should I give it when you call .show()?" problem,
making it extremely clear that something needed to be done on our part.
Changes
Changes since the 20 Feburary 2014 Working Draft include:
- Renamed display-box to 'box-suppress' and display-extras to 'display-list'.
- Moved the ''display-outside/contents'' value from 'box-suppress' to 'display-outside'.
- Added ''hide'' value to 'box-suppress'.
- Added blockification and inlinification rules.
- Added ''display: run-in'' and defined its layout in [[#run-in]].
- Various editorial improvements.
- Added [[#glossary]], incorporating key CSS2.1 terms.