display
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
ThedisplayCSS property sets whether an element is treated as ablock or inline box and the layout used for its children, such asflow layout,grid orflex.
Formally, thedisplay property sets an element's inner and outerdisplay types. The outer type sets an element's participation inflow layout; the inner type sets the layout of children. Some values ofdisplay are fully defined in their own individual specifications; for example the detail of what happens whendisplay: flex is declared is defined in the CSS Flexible Box Model specification.
In this article
Try it
display: block;display: inline-block;display: none;display: flex;display: grid;<p> Apply different <code>display</code> values on the dashed orange-bordered <code>div</code>, which contains three child elements.</p><section> <div> Some text A. <div> <div>Child 1</div> <div>Child 2</div> <div>Child 3</div> </div> Some text B. </div></section>.example-container { width: 100%; height: 100%;}code { background: #88888888;}#example-element { border: 3px dashed orange;}.child { display: inline-block; padding: 0.5em 1em; background-color: #ccccff; border: 1px solid #ababab; color: black;}Syntax
/* precomposed values */display: block;display: inline;display: inline-block;display: flex;display: inline-flex;display: grid;display: inline-grid;display: flow-root;/* Box suppression */display: none;display: contents;/* multi-keyword syntax */display: block flex;display: block flow;display: block flow-root;display: block grid;display: inline flex;display: inline flow;display: inline flow-root;display: inline grid;/* 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: revert-layer;display: unset;The CSSdisplay property is specified using keyword values.
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:
blockThe element generates a block box, generating line breaks both before and after the element when in the normal flow.
inlineThe element generates one or more inline 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:When browsers that support multi-keyword syntax encounter a display property that only has anouter value (e.g.,display: block ordisplay: inline), the inner value is set toflow (e.g.,display: block flow anddisplay: inline flow).
Note:To be sure layouts work on older browsers, you may use single-value syntax, for exampledisplay: inline flex could have the following fallback
.container { display: inline-flex; display: inline flex;}SeeUsing the multi-keyword syntax with CSS display for more information.
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). When one of these keywords is used by itself as a single value, the element's outer display type defaults to
block(with the exception ofruby, which defaults toinline).flowThe element lays out its contents using flow layout (block-and-inline layout).
If its outer display type is
inline, and it is participating in a block or inline formatting context, then it generates an inline box. Otherwise it generates a block 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 newblock formatting context (BFC) for its contents or integrates its contents into its parent formatting context.flow-rootThe element generates a block box that establishes a newblock formatting context, defining where the formatting root lies.
tableThese elements behave like HTML
<table>elements. It defines a block-level box.flexThe element behaves like a block-level element and lays out its content according to theflexbox model.
gridThe element behaves like a block-level element and lays out its content according to thegrid model.
rubyThe element behaves like an inline-level element and lays out its content according to the ruby formatting model. It behaves like the corresponding HTML
<ruby>elements.
Note:When browsers that support multi-keyword syntax encounter a display property that only has aninner value (e.g.,display: flex ordisplay: grid), the outer value is set toblock (e.g.,display: block flex anddisplay: block grid).
List Item
<display-listitem>The element generates a block box for the content and a separate list-item inline box.
A single value oflist-item will cause the element to behave like a list item.This can be used together withlist-style-type andlist-style-position.
list-item can also be combined with any<display-outside> keyword and theflow orflow-root<display-inside> keyword.
Note:In browsers that support the multi-keyword syntax, if no inner value is specified, it will default toflow.If no outer value is specified, the principal box will have an outer display type ofblock.
Internal
<display-internal>Some layout models such as
tableandrubyhave 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.table-row-groupThese elements behave like
<tbody>HTML elements.table-header-groupThese elements behave like
<thead>HTML elements.table-footer-groupThese elements behave like
<tfoot>HTML elements.table-rowThese elements behave like
<tr>HTML elements.table-cellThese elements behave like
<td>HTML elements.table-column-groupThese elements behave like
<colgroup>HTML elements.table-columnThese elements behave like
<col>HTML elements.table-captionThese elements behave like
<caption>HTML elements.ruby-baseThese elements behave like
<rb>HTML elements.ruby-textThese elements behave like
<rt>HTML elements.ruby-base-containerThese elements are generated as anonymous boxes.
ruby-text-containerThese elements behave like
<rtc>HTML elements.
Box
<display-box>These values define whether an element generates display boxes at all.
contentsThese 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
contentsvalue should affect "unusual elements" — elements that aren't rendered purely by CSS box concepts such as replaced elements. SeeAppendix B: Effects of display: contents on Unusual Elements for more details.noneTurns 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
visibilityproperty instead.
Precomposed
<display-legacy>CSS 2 used a single-keyword, precomposed syntax for the
displayproperty, requiring separate keywords for block-level and inline-level variants of the same layout mode.inline-blockThe element generates a block 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-tableThe
inline-tablevalue 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-flexThe element behaves like an inline-level element and lays out its content according to the flexbox model.
It is equivalent to
inline flex.inline-gridThe element behaves like an inline-level element and lays out its content according to the grid model.
It is equivalent to
inline grid.
Which syntax should you use?
TheCSS display module describes a multi-keyword syntax for values you can use with thedisplay property to explicitly defineouter andinner display.The single keyword values (precomposed<display-legacy> values) are supported for backward-compatibility.
For example, using two values you can specify an inline flex container as follows:
.container { display: inline flex;}This can also be specified using the legacy single value:
.container { display: inline-flex;}For more information on these changes, see theUsing the multi-keyword syntax with CSS display guide.
Description
The individual pages for the different types of value thatdisplay can have set on it feature multiple examples of those values in action — see theSyntax section. In addition, see the following material, which covers the various values of display in depth.
Multi-keyword values
CSS Flow Layout (display: block, display: inline)
display: flex
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
- Aligning items in CSS grid layout
- Grids, logical values and writing modes
- CSS grid layout and accessibility
- Realizing common layouts using grids
Animating display
Supporting browsers animatedisplay with adiscrete animation type. This generally means that the property will flip between two values 50% through animating between the two.
There is one exception, which is when animating to or fromdisplay: none. In this case, the browser will flip between the two values so that the animated content is shown for the entire animation duration. So for example:
- When animating
displayfromnonetoblock(or another visibledisplayvalue), the value will flip toblockat0%of the animation duration so it is visible throughout. - When animating
displayfromblock(or another visibledisplayvalue) tonone, the value will flip tononeat100%of the animation duration so it is visible throughout.
This behavior is useful for creating entry/exit animations where you want to for example remove a container from the DOM withdisplay: none, but have it fade out withopacity rather than disappearing immediately.
When animatingdisplay withCSS animations, you need to provide the startingdisplay value in an explicit keyframe (for example using0% orfrom). SeeUsing CSS animations for an example.
When animatingdisplay withCSS transitions, two additional features are needed:
@starting-styleprovides starting values for properties you want to transition from when the animated element is first shown. This is needed to avoid unexpected behavior. By default, CSS transitions are not triggered on an element's first style update or when thedisplaytype changes fromnoneto another type.transition-behavior: allow-discreteneeds to be set on thetransition-propertydeclaration (or thetransitionshorthand) to enabledisplaytransitions.
For examples of transitioning thedisplay property, see the@starting-style andtransition-behavior pages.
Accessibility
>display: none
Using adisplay value ofnone on an element will remove it from theaccessibility 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 usea combination of properties to remove it visually from the screen but still make it available to assistive technology such as screen readers.
Whiledisplay: none hides content from the accessibility tree, elements that are hidden but are referenced from visible elements'aria-describedby oraria-labelledby attributes are exposed to assistive technologies.
display: contents
Current implementations in some browsers will remove from theaccessibility tree any element with adisplay value ofcontents (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 theCSS specification.
Tables
In some browsers, changing thedisplay value of a<table> element toblock,grid, orflex will alter its representation in theaccessibility tree. This will cause the table to no longer be announced properly by screen reading technology.
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 | Discrete behavior except when animating to or fromnone is visible for the entire duration |
Formal syntax
display =
[<display-outside>||<display-inside>]|
<display-listitem>|
<display-internal>|
<display-box>|
<display-legacy>|
<display-outside>||[<display-inside>|math]
<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
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 differentdisplay 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 have includedpadding andbackground-color on the containers and their children, so that it is easier to see the effect the display values are having.
HTML
<article> <span>First</span> <span>Second</span> <span>Third</span></article><article> <span>First</span> <span>Second</span> <span>Third</span></article><div> <label for="display">Choose a display value:</label> <select> <option selected>block</option> <option>block flow</option> <option>inline</option> <option>inline flow</option> <option>flow</option> <option>flow-root</option> <option>block flow-root</option> <option>table</option> <option>block table</option> <option>flex</option> <option>block flex</option> <option>grid</option> <option>block grid</option> <option>list-item</option> <option>block flow list-item</option> <option>inline flow list-item</option> <option>block flow-root list-item</option> <option>inline flow-root list-item</option> <option>contents</option> <option>none</option> <option>inline-block</option> <option>inline flow-root</option> <option>inline-table</option> <option>inline table</option> <option>inline-flex</option> <option>inline flex</option> <option>inline-grid</option> <option>inline grid</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 that some multi-keyword values are added for illustration which have the following equivalents:
block=block flowinline=inline flowflow=block flowflow-root=block flow-roottable=block tableflex=block flexgrid=block gridlist-item=block flow list-iteminline-block=inline flow-rootinline-table=inline tableinline-flex=inline flexinline-grid=inline grid
You can find more examples in the pages for each separate display type underGrouped values.
Specifications
| Specification |
|---|
| CSS Display Module Level 3> # the-display-properties> |
| Scalable Vector Graphics (SVG) 2> # VisibilityControl> |