display
Determines how an element behaves in the page layout and how it lays out its children.
- Initial value
inline- Inherited
- No
- Animatable
- no, it switches instantly between values
- Applies to
- all elements
- JavaScript
element.style.display = "flex"
Example
#.menu {
display: flex;
gap: 16px;
}
.menu a {
display: inline-block;
padding: 8px 14px;
}<nav class="menu stage">
<a href="#">Courses</a>
<a href="#">Documentation</a>
<a href="#">Collections</a>
</nav>Definition and usage
#The display property is the foundation of every layout. It tells the browser two things: how the element behaves on the outside (does it start a new line like a block, or flow within text like an inline element) and how it lays out its children (normal flow, Flexbox, Grid).
Block elements, such as <div> or <p>, take up all the available width and start on a new line. Inline elements, such as <span> or <a>, flow within text and ignore width and height. inline-block combines both worlds: the element sits inline, but you can still give it dimensions.
The values flex and grid turn an element into a modern layout container, and none removes the element from the page entirely, including the space it used to take up.
Syntax
#display: block | inline | inline-block | flex | inline-flex | grid | inline-grid | none | contents | …;Values
#- Elements flow inline like text. The
110pxwidth is ignored. - Every element starts a new line and respects the width.
- Elements sit on a single line, but have their own width and vertical margins.
- The container lays out its children in a flexible row. Here the value goes on the parent.
- The container lays out its children in a grid. Here, three equal columns.
- The element disappears and takes up no space. Here we hide the second element.
Values in practice
#inline
110px width is ignored..item { display: inline; }block
.item { display: block; }inline-block
.item { display: inline-block; }flex
.stage { display: flex; gap: 6px; }grid
.stage { display: grid; grid-template-columns: repeat(3, 1fr); }
.item { width: auto; }none
.item:nth-child(2) { display: none; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If
widthormargin-topdoesn’t work on an element, check whether it’s inline.inline-blockorblockwill help. - To hide an element only visually, while keeping it available to screen readers, don’t use
display: none.
Related entries
#- display vs visibility vs opacitydisplay: none removes an element from the layout, visibility: hidden hides it but keeps its space, and opacity: 0 makes it transparent, yet still clickable.
- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
- visibilityHides an element but leaves behind the empty space it occupied.
- flex-directionSets the direction of the main axis: flex items line up in a row or in a column.
- grid-template-columnsDefines the grid’s columns: their number and widths.
- floatMoves an element to the left or right edge, and text wraps around it on the other side.
Spotted a mistake or missing an example? Write to us.