display
The 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.
Syntax
The CSS display
property is specified using keyword values.
/* legacy values */ display: block; display: inline; display: inline-block; display: flex; display: inline-flex; display: grid; display: inline-grid; display: flow-root; /* box generation */ display: none; display: contents; /* two-value syntax */ display: block flow; display: inline flow; display: inline flow-root; display: block flex; display: inline flex; display: block grid; display: inline grid; display: block flow-root; /* other values */ display: table; display: table-row; /* all table elements have an equivalent CSS display value */ display: list-item; /* Global values */ display: inherit; display: initial; display: revert; display: unset;
Grouped values
The keyword values can be grouped into six value categories.
Outside
<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
Note: 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.
Inside
<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
-
The element lays out its contents using flow layout (block-and-inline layout).
If its outer display type is
inline
orrun-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
, oroverflow
) 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>
-
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.
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>
-
Some layout models such as
table
andruby
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
-
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>
-
These values define whether an element generates display boxes at all.
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 used a single-keyword syntax for the
display
property, requiring separate keywords for block-level and inline-level variants of the same layout mode.
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
.
Which syntax should you use now?
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.
The <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
/* Global values */ display: inherit; display: initial; display: unset;
Description
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 Syntax section. In addition, see the following material, which covers the various values of display in depth.
CSS Flow Layout (display: block, display: inline)
- Block and Inline Layout in Normal Flow
- Flow Layout and Overflow
- Flow Layout and Writing Modes
- Formatting Contexts Explained
- In Flow and Out of Flow
display: flex
- Basic concepts of flexbox
- Aligning Items in a Flex Container
- Controlling Ratios of Flex Items Along the Main Axis
- Cross-browser Flexbox mixins
- Mastering Wrapping of Flex Items
- Ordering Flex Items
- Relationship of flexbox to other layout methods
- Backwards Compatibility of Flexbox
- Typical use cases of Flexbox
display: grid
- Basic Concepts of Grid Layout
- Relationship to other layout methods
- Line-based placement
- Grid template areas
- Layout using named grid lines
- Auto-placement in grid layout
- Box alignment in grid layout
- Grids, logical values and writing modes
- CSS Grid Layout and Accessibility
- CSS Grid Layout and Progressive Enhancement
- Realizing common layouts using grids
Accessibility concerns
display: none
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 parsable by assistive technology such as screen readers.
display: contents
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.
- More accessible markup with display: contents | Hidde de Vries
- Display: Contents Is Not a CSS Reset | Adrian Roselli
Tables
Changing the display
value of a <table>
element to block
, grid
, 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.
- Short note on what CSS display properties do to table semantics — The Paciello Group
- Hidden content for better a11y | Go Make Things
- MDN Understanding WCAG, Guideline 1.3 explanations
- Understanding Success Criterion 1.3.1 | W3C Understanding WCAG 2.0
Formal definition
Initial value | inline |
---|---|
Applies to | all elements |
Inherited | no |
Computed value | 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 type | Not animatable |
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
Examples
display value comparison
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.
Note: We've not included any of the modern two-value syntax, as support for that is still fairly limited.
HTML
<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>
CSS
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; }
JavaScript
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();
Result
Note: You can find more examples in the pages for each separate display data type, linked above.
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
display |
1 |
12 |
1 |
4 |
7 |
1 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
contents |
65 |
79 |
37 |
No |
52 |
11.1 |
65 |
65 |
37 |
47 |
11.3 |
9.0 |
display-outside |
1 |
12 |
1 |
4 |
7 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
flex |
29
21
|
12 |
20
Firefox 28 added multi-line flexbox support.
|
11
IE incorrectly positions inline block content inside flex containers. See the discussion on Microsoft Answers.
8
IE incorrectly positions inline block content inside flex containers. See the discussion on Microsoft Answers.
|
16
15
12.1-15
|
9
7
|
4.4
≤37
|
29
25
|
20
Firefox 28 added multi-line flexbox support.
|
16
14
12.1-14
|
9
7
|
2.0
1.5
|
flow-root |
58 |
79 |
53 |
No |
45 |
13 |
58 |
58 |
53 |
43 |
13 |
7.0 |
grid |
57 |
16
12
|
52 |
10
Internet Explorer implements an older version of the specification.
|
44 |
10.1 |
57 |
57 |
52 |
43 |
10.3 |
6.0
Samsung Internet added this earlier than the corresponding Chrome version would indicate.
|
inline-block |
1 |
12 |
1 |
8
6
Until Internet Explorer 8,
inline-block is only for natural inline elements. |
7 |
1 |
≤37 |
18 |
4 |
14 |
1 |
1.0 |
inline-flex |
29
21
|
12 |
20
Firefox 28 added multi-line flexbox support.
|
11
8
|
16
15
|
9
6.1
|
4.4
≤37
|
29
25
|
20
Firefox 28 added multi-line flexbox support.
|
16
14
|
9
6.1
|
2.0
1.5
|
inline-grid |
57 |
16
12
|
52 |
10
Internet Explorer implements an older version of the specification.
|
44 |
10.1 |
57 |
57 |
52 |
43 |
10.3 |
6.0
Samsung Internet added this earlier than the corresponding Chrome version would indicate.
|
inline-table |
1 |
12 |
3 |
8 |
7 |
1 |
≤37 |
18 |
4 |
14 |
1 |
1.0 |
list-item |
1 |
12 |
1 |
6 |
7 |
1 |
≤37 |
18 |
4 |
14 |
1 |
1.0 |
multi-keyword_values |
No |
No |
70 |
No |
No |
No |
No |
No |
No |
No |
No |
No |
none |
1
Chrome 65 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
12 |
1 |
4 |
7
Opera 52 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
1 |
≤37
WebView 65 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
18
Chrome 65 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
4 |
10.1
Opera Android 47 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
1 |
1.0
Chrome 65 stopped creating layout objects for elements inside an
<iframe> with display:none applied. |
ruby_values |
No |
12-79 |
38 |
7 |
No |
No |
No |
No |
38 |
No |
No |
No |
table_values |
1 |
12 |
1 |
8 |
7 |
1 |
≤37 |
18 |
4 |
14 |
1 |
1.0 |
xul_box_values |
No |
No |
1-64
This is still available to Firefox UI code.
62
|
No |
No |
No |
No |
No |
4-64
This is still available to Firefox UI code.
62
|
No |
No |
No |
xul_deck_values |
No |
No |
1-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
No |
No |
4-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
xul_grid_values |
No |
No |
1-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
No |
No |
4-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
xul_inline_grid_stack |
No |
No |
1-62
Available to Firefox UI code.
62-70
|
No |
No |
No |
No |
No |
4-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
xul_popup_values |
No |
No |
1-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
No |
No |
4-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
xul_stack_value |
No |
No |
1-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
No |
No |
4-62
This is still available to Firefox UI code.
62
|
No |
No |
No |
See also
- Block and inline layout in normal flow
- Introduction to formatting contexts
-
visibility
,float
,position
-
grid
,flex
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/display