Skip to content
Tutorials Center
My learning

Topics

Lesson 57 of 139 ยท Layout Fundamentals

The display Property

Learn how the CSS display property controls layout, from block and inline boxes to inline-block, none, flow-root and contents, plus the outer and inner display model.

Before CSS can place anything on the page, it has to know what kind of box each element generates. Should a heading start on a new line and stretch across its container? Should a link sit inside a sentence? Should a list lay its children out in a row? The display property answers all of these questions, which makes it the starting point of every layout.

You already met the idea in the HTML lesson on block and inline elements. In this lesson you will see what display really controls, meet each common value, and learn the modern "two-value" model that explains how flex, grid and everything else fit together.

Outer and inner display

display actually sets two things about a box:

  • The outer display type: how the box itself takes part in the layout of its parent. It is either block (starts on a new line, takes the full width) or inline (flows within a line of text).
  • The inner display type: how the box lays out its own children. The default is flow (normal block-and-inline layout), but it can also be flex, grid, table and others.

Modern CSS lets you write both parts explicitly, and the familiar one-word values are shorthands for pairs:

Short value Two-value form Meaning
block block flow Block outside, normal flow inside
inline inline flow Inline outside, normal flow inside
inline-block inline flow-root Inline outside, a self-contained block inside
flow-root block flow-root Block outside, self-contained inside
flex block flex Block outside, flexbox inside
inline-flex inline flex Inline outside, flexbox inside
grid block grid Block outside, grid inside
inline-grid inline grid Inline outside, grid inside

The two-value syntax works in all current browsers, but the one-word forms are what you will see in almost every codebase, so this course uses them. The table is still worth remembering: inline-flex is not a strange special case, it is just "inline on the outside, flex on the inside."

Every element starts with a default display from the browser's own stylesheet. <div>, <p>, <h1> and <section> are block; <span>, <a>, <strong> and <img> are inline; <li> is list-item; table elements use the table-* values. CSS can change any of them, and the change never affects what the element means. A <span> with display: block is still a span to a screen reader.

block and inline

A block box:

  • starts on a new line, and the next sibling also starts on a new line;
  • stretches to fill the width of its container unless you set a width;
  • respects width, height, margin and padding on all sides.

An inline box:

  • sits within a line of text and wraps across lines like words;
  • ignores width and height; its size comes from its content;
  • takes horizontal margins and padding into account, but vertical padding does not push lines apart and vertical margins are ignored.

The last point surprises many people. Try it:

Block versus inline boxes

The whitespace gap. Because inline-block elements are part of a line of text, the spaces and line breaks between them in your HTML show up as small gaps (about 4px). For rows of boxes, use flexbox with gap instead; it ignores whitespace and gives you exact control.

display: none

display: none removes the element from the page completely. It generates no box, takes up no space, cannot be clicked or focused, and is hidden from screen readers. The same goes for all its children; no child can override it.

Compare this with two other ways to hide things:

Technique Takes up space? Clickable? Read by screen readers?
display: none No No No
visibility: hidden Yes, an empty gap No No
opacity: 0 Yes Yes Yes

Use display: none (or the HTML hidden attribute, which applies it) for things that should not exist right now: closed menus, inactive tabs, content for another screen size. When something must be hidden visually but still read aloud, use a "visually hidden" class instead, which you will build in Accessible CSS.

flow-root: a box that contains its children

display: flow-root makes a block box that creates a new block formatting context. That jargon means the box becomes a self-contained layout island. Two practical effects follow:

  • It grows to contain floated children. Without it, a container with only floated children collapses to zero height.
  • The margins of its children no longer collapse through it. A child's top margin stays inside the parent instead of leaking out above it (see margin collapsing).
flow-root contains floats and margins

Remove .fixed from the last box and its child's margin escapes, so the dashed border hugs the text. You will use flow-root again in Floats. Flex and grid containers also create a formatting context, which is one reason float and margin problems mostly vanish inside them.

display: contents

display: contents makes the element's own box disappear, while its children stay and act as if they were children of the grandparent. Its background, border and padding vanish with it.

That is useful when a wrapper element gets in the way of a flex or grid layout:

HTML
<ul class="grid">
  <li>One</li>
  <li class="group" style="display: contents">
    <span>Two</span>
    <span>Three</span>
  </li>
  <li>Four</li>
</ul>

If .grid is a grid container, Two and Three become grid items directly, sitting in the grid like One and Four.

Use contents carefully on semantic elements. Older browser versions dropped elements with display: contents from the accessibility tree, so a <button> or a list could lose its role. Current browsers have fixed most of these bugs, but avoid it on buttons, links, tables and headings, and test with a screen reader. On neutral wrappers like <div> it is safe. Subgrid is often the better tool for aligning nested content.

Changing display in practice

You will change display all the time, usually for one of these reasons:

  • Make a link fill a menu item. Navigation links are inline by default, so only the text is clickable. display: block (or inline-block for a row) lets them take padding and a bigger hit area.
  • Remove the gap under images. An <img> is inline, so it sits on the text baseline and leaves a few pixels of space below it for letter descenders. display: block on the image removes that gap.
  • Turn on a layout model. display: flex or display: grid on a container is the first line of nearly every modern layout.
  • Show and hide. Toggle between none and a visible value with a class or the hidden attribute.

Pick the value by asking two questions: how should this box sit among its neighbors, and how should it arrange its own children? The answers are your outer and inner display types.

Other values you will meet

  • flex and inline-flex turn on flexbox for the children. Covered in the Flexbox chapter.
  • grid and inline-grid turn on grid layout. Covered in the Grid chapter.
  • list-item generates a block box plus a marker, which is how <li> gets its bullet. See Styling Lists.
  • table, table-row, table-cell and friends make non-table elements behave like table parts. Grid has replaced most uses of them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You set width: 150px on a <span> and nothing changes. Why?

    Inline boxes size themselves from their content. Make it inline-block or block for width to apply.
  2. 2. Which statement about display: none is true?

    display: none generates no box at all, so nothing inside it can be seen, focused or read.
  3. 3. What is inline-flex in the two-value syntax?

    The first value is the outer type (inline), the second is the inner layout for the children (flex).
  4. 4. A container has only floated children and collapses to zero height. Which value fixes it with no side effects?

    flow-root creates a new block formatting context, which grows to contain floats.

Summary

  • display sets an outer type (how the box sits among its siblings) and an inner type (how it lays out its children).
  • block boxes start new lines, fill the width and respect all sizes; inline boxes flow with text and ignore width, height and vertical margins.
  • inline-block sits in a line but sizes like a block; good for badges and keys.
  • display: none removes an element completely; visibility: hidden and opacity: 0 keep its space.
  • flow-root contains floats and stops margins collapsing through; contents removes a wrapper's box but keeps its children.

Next: Opacity and Visibility.