The display property
display sets an element's box type: whether it starts on a new line, sits inside a line of text, lays out its children as a flex or grid container — or is not shown at all.
Syntax
display: block | inline | inline-block | flex | inline-flex | grid | inline-grid | none | … - Initial value
inline- Inherited
- no
- Applies to
- all elements
Values
| Value | What it does |
|---|---|
block | starts on a new line and takes the full available width; width, height and vertical margins apply |
inline | sits inside a line of text; width and height have no effect, vertical margins do not push neighbouring lines |
inline-block | sits in a line like text, but width, height, margin and padding work as for a block |
flex | a block-level flex container: direct children become flex items |
inline-flex | the same as flex, but the container itself sits inline |
grid | a block-level grid container: children are placed in grid rows and columns |
inline-grid | the same as grid, but the container itself sits inline |
none | the element is not rendered and takes no space; screen readers skip it too |
Example
span {
background: #e0f2fe;
border: 1px solid #0284c7;
padding: 4px 8px;
}
.inline { width: 200px; }
.inline-block { display: inline-block; width: 200px; }
.block { display: block; margin-top: 12px; } <p>Text with <span class="inline">inline</span> and <span class="inline-block">inline-block</span> on one line.</p>
<span class="block">block — on a new line, full width</span> Result
Good to know
- The initial value in the spec is inline, but browsers set display: block for div, p, h1–h6, ul, section and other block tags themselves. That is why span and a sit in a line and div does not.
- display: none removes the element entirely. To hide it but keep its space, use visibility: hidden.
- The hidden attribute hides an element through display: none in the browser's styles, so a rule such as display: flex on the same element overrides hidden. Add [hidden] { display: none !important; } or don't set display on hidden elements.
Sources and specification
- MDN: the display property — MDN Web Docs
- CSS Display Module Level 3 (W3C) — W3C
See also
CSS Flexbox for beginners
A beginner's guide to CSS Flexbox: display: flex, axes, justify-content, align-items, flex-wrap, gap and flex: 1, with examples of a menu, cards and centering.
CSS Flexbox Playground
Change flex-direction, justify-content, align-items, flex-wrap and gap and see how flexbox works right away. Copy the ready CSS code in one click.
The position property
The CSS position property: how static, relative, absolute, fixed and sticky work, what top and left are measured from and why sticky stops working.
The justify-content property
CSS justify-content distributes space between items along the main axis in flexbox and grid: flex-start, center, space-between, space-evenly.
The hidden attribute
The global boolean hidden attribute hides an element from both sighted users and screen readers.
Linking CSS
Lesson 11: three ways to add CSS — an external file via <link rel="stylesheet">, a <style> block in head and the style attribute. Example and task.