docs.rodeo

MDN Web Docs mirror

display

{{CSSRef}} 

The display CSS property sets whether an element is treated as a block or inline box 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.

{{EmbedInteractiveExample("pages/css/display.html")}} 

Syntax

/* precomposed 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;

/* 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 CSS display property is specified using keyword values.

Grouped values

The keyword values can be grouped into six value categories.

Outside

[!NOTE] When browsers that support multi-keyword syntax encounter a display property that only has an outer value (e.g., display: block or display: inline), the inner value is set to flow (e.g., display: block flow and display: inline flow).

[!NOTE] To be sure layouts work on older browsers, you may use single-value syntax, for example display: inline flex could have the following fallback

.container {
  display: inline-flex;
  display: inline flex;
}

See Using the multi-keyword syntax with CSS display for more information.

Inside

[!NOTE] When browsers that support multi-keyword syntax encounter a display property that only has an inner value (e.g., display: flex or display: grid), the outer value is set to block (e.g., display: block flex and display: block grid).

List Item

A single value of list-item will cause the element to behave like a list item. This can be used together with {{CSSxRef("list-style-type")}}  and {{CSSxRef("list-style-position")}} .

list-item can also be combined with any {{CSSxRef("<display-outside>")}}  keyword and the flow or flow-root {{CSSxRef("<display-inside>")}}  keyword.

[!NOTE] In browsers that support the multi-keyword 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

Box

Precomposed

Which syntax should you use?

The CSS display module describes a multi-keyword syntax for values you can use with the display property to explicitly define outer and inner 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 the Using the multi-keyword syntax with CSS display guide.

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.

Multi-keyword values

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

display: flex

display: grid

Animating display

Supporting browsers animate display with a discrete 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 from display: 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:

This behavior is useful for creating entry/exit animations where you want to for example remove a container from the DOM with display: none, but have it fade out with opacity rather than disappearing immediately.

When animating display with CSS animations, you need to provide the starting display value in an explicit keyframe (for example using 0% or from). See Using CSS animations for an example.

When animating display with CSS transitions, two additional features are needed:

For examples of transitioning the display property, see the @starting-style and transition-behavior pages.

Accessibility

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 still make it available to assistive technology such as screen readers.

While display: none hides content from the accessibility tree, elements that are hidden but are referenced from visible elements’ aria-describedby or aria-labelledby attributes are exposed to assistive technologies.

display: contents

Current implementations in some 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.

Tables

In some browsers, changing the display value of a {{HTMLElement("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.

Formal definition

{{cssinfo}} 

Formal syntax

{{csssyntax}} 

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 have included {{cssxref("padding")}}  and {{cssxref("background-color")}}  on the containers and their children, so that it is easier to see the effect the display values are having.

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

{{EmbedLiveSample('display_value_comparison','100%', 440)}} 

Note that some multi-keyword values are added for illustration which have the following equivalents:

You can find more examples in the pages for each separate display type under Grouped values.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN