CSS Reference ยท Box Model and Sizing
The display property
Reference for the CSS display property: block, inline, inline-block, flex, grid, contents, none and the two-value syntax, with live examples and usage notes.
The display property decides what kind of box an element generates. It controls two things at once: how the element behaves outside (does it start on a new line and fill the available width, or flow within a line of text?) and how it lays out its children inside (normal flow, flexbox, grid, a table, and so on). It can also remove the box completely.
Every element has a default value from the browser's style sheet, for example block for <div> and <p>, and inline for <span> and <a>.
.menu { display: flex; } .hidden { display: none; }
Try it
Syntax
display: [ <display-outside> || <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy>; /* Examples of the two-value form */ display: block flex; /* same as flex */ display: inline grid; /* same as inline-grid */ display: block flow-root; /* same as flow-root */
- Initial value:
inline - Applies to: all elements
- Inherited: no
- Animatable: no (discrete: it flips at the midpoint, or use
transition-behavior: allow-discrete) - Computed value: as specified, except that floating or absolutely positioned elements, and the root, are blockified (
inlinebecomesblock, and so on)
Values
| Value | Description |
|---|---|
block |
Starts on a new line and takes the full width available. Width, height, margins and padding all apply. |
inline |
Flows within a line of text. Width and height are ignored, and vertical margins have no layout effect. |
inline-block |
Flows inline like text, but its inside behaves like a block, so width and height work. |
flex |
A block-level flex container; the children become flex items. |
inline-flex |
The same, but the container itself flows inline. |
grid |
A block-level grid container. |
inline-grid |
An inline-level grid container. |
flow-root |
A block container that starts a new formatting context. It contains floats and stops margins collapsing through it. |
contents |
The element's own box disappears; its children are laid out as if they were children of the parent. |
list-item |
A block box with a marker, like <li>. |
table, table-row, table-cell, ... |
Table layout behaviors, without using table markup. |
none |
No box is generated at all. The element and its descendants take no space and are not rendered. |
Usage notes
display: nonehides from everyone. The element is removed from the accessibility tree too. Usevisibility: hiddento keep the space, or thehiddenattribute for semantic hiding.- Setting
flexorgridaffects children, not the element itself. The element still takes the full width because its outer type isblock; useinline-flexorinline-gridif you want it shrunk to fit. display: contentsand accessibility. It removes the box, and in the past some browsers also dropped the element's semantics (for buttons, lists and tables). Test with a screen reader before using it on semantic elements.- Changing
displayof table elements can strip their table roles in some assistive technologies, so avoid restyling real tables as blocks without adding ARIA roles. - Whitespace gaps. Inline and inline-block elements are separated by a space when there is whitespace between tags in the HTML. Flex and grid do not have that gap.
- Animating in and out.
displaycan transition only withtransition-behavior: allow-discrete, and entry effects need@starting-style.
Browser support
The classic keywords are supported everywhere. The two-value syntax and flow-root are supported by all current browsers, and contents is supported with the accessibility caveat noted above.
Related
visibilityโ hide while keeping the spacefloatโ takes an element out of normal flowflexโ flex item sizinggridโ grid shorthand- Lesson: The display Property
- Lesson: Flexbox