Chapter 15 of 33

The Display Property

Why does a <div> always start on a new line while a <span> sits inside a sentence? Why does width: 300px do nothing on a link? Why is there a mysterious gap under every image? All three questions have the same answer: the display property.

display is the starting point of all CSS layout. It decides what kind of box an element generates and how that box behaves among its neighbours. In this chapter we'll study block, inline, inline-block and none in depth, compare ways of hiding elements, and get a first look at flex, grid, flow-root and contents. You should be comfortable with The Box Model and Margin & Padding before reading this.


What Does display Do?

The display property defines the type of box an element generates. It has two parts: the outer display type (how the box takes part in its parent's layout — block or inline) and the inner display type (how the box lays out its own children — normal flow, flex, grid, table ...).

                 display: inline-flex
                          /        \
              outer: inline         inner: flex
   "I sit in a line of text     "my children are laid
    like a word"                 out with flexbox"

Every element has a default display value that comes from the browser's built-in stylesheet. We checked these with getComputedStyle() in Chrome:

Default displayElements
blockdiv, p, h1–h6, ul, ol, section, article, header, footer, main, nav, form, details
inlinespan, a, strong, em, code, label, img, video, iframe, canvas
inline-blockbutton, input, select, textarea
list-itemli, summary (the clickable heading of details)
table, table-row, table-celltable, tr, td/th
nonehead, script, style, template, a closed dialog

An important point: display changes only the look, not the meaning. A <span> with display: block is still a span to screen readers and search engines, and a link with display: block is still a link. Choose HTML elements for meaning, then use display for layout.


display: block

Block boxes stack vertically, like paragraphs in a document.

  • Starts on a new line, and the next element also starts on a new line.

  • By default, stretches to fill the full available width of its parent (its width is auto, not 100% — so padding and border fit inside without overflowing).

  • width, height, margin and padding all work on all sides.

  • Vertical margins between adjacent blocks can collapse (from Margin & Padding).

  • margin-inline: auto centres a block that has a set width.

.card {
  display: block;    /* already the default for div */
  width: 300px;
  margin: 20px auto; /* centred, 20px above and below */
  padding: 16px;
}

A common use is making a whole link clickable as a large area:

.menu a {
  display: block;    /* the entire row is clickable, not just the words */
  padding: 12px 16px;
}

display: inline

Inline boxes flow along a line of text, like words in a sentence. When the line is full, they wrap onto the next line — an inline box can even be split across two lines.

  • Does not start a new line.

  • Takes only as much width as its content.

  • width and height are ignored.

  • Horizontal margin, padding and border work and push neighbouring text away.

  • Vertical margin is ignored. Vertical padding and border are painted, but do not push lines apart, so they can overlap the lines above and below.

Let's prove it. This span has a size, margins and padding:

span.tag {
  width: 300px;
  height: 100px;
  margin: 20px;
  padding: 10px;
  border: 2px solid #3366cc;
  background: #ccddee;
}
<p>Text before <span class="tag">inline span</span> text after ...</p>

Measured in Chrome (Arial 16px):

Rendered box: 100.5px × 41px
  width  = text width (~76.5) + padding 10+10 + border 2+2   ← width: 300px ignored
  height = line of text (~17) + padding 10+10 + border 2+2   ← height: 100px ignored
Horizontal margins of 20px: visible gaps on both sides
Vertical margins of 20px: no effect; the line spacing is unchanged

The blue background box extends above and below the line, but the paragraph's lines don't move to make room for it. That's why "my padding on a link overlaps the text above" is such a common surprise.


display: inline-block

inline-block is a hybrid: outside it behaves like inline (sits on the line next to other content), but inside it behaves like a block (it respects width, height, and all margins and padding).

.badge {
  display: inline-block;
  width: 100px;
  padding: 6px 0;
  margin: 4px;
  text-align: center;
  background: #ffcc99;
}

Now the width works, vertical padding and margin push other lines away properly, and the badges still sit side by side. Buttons and form inputs are inline-block by default for exactly this reason: they must fit into a line of text but need a real size.

Comparison

Behaviourblockinlineinline-block
Starts on a new lineYesNoNo
Default widthFills the parentFits contentFits content
width / heightRespectedIgnoredRespected
Vertical marginRespected (can collapse)IgnoredRespected (never collapses)
Vertical padding/borderPushes contentPainted, but doesn't affect line spacingPushes content
Can break across linesNoYesNo (moves to the next line as a whole)
Typical elementsdiv, p, h1span, a, embutton, input

The gap between inline-block elements

Put three inline-block boxes next to each other in HTML:

<div class="ib">A</div> <div class="ib">B</div><div class="ib">C</div>

In Chrome, there was a 4.45px gap between A and B and 0px between B and C. Why? Inline-block boxes are treated like words, and the space (or newline) in the HTML between A and B is rendered as a normal space character. In 16px Arial a space is about 4.45px wide. Between B and C there was no whitespace in the source, so no gap.

This gap breaks layouts like "four boxes of exactly 25% each" (they no longer fit on one line). Old fixes included removing whitespace or setting font-size: 0 on the parent. The modern fix is simply to use flexbox or grid for rows of boxes, which ignore that whitespace.

The gap under images

Images are inline by default, and inline content sits on the text baseline. Below the baseline the line keeps room for descenders (the tails of letters like g, p, y). So a 50px-tall image inside a div made the div 54px tall in our test (Arial 16px), with 4px of empty space under the image.

/* Fix 1: make the image a block (most common) */
img {
  display: block;
}

/* Fix 2: keep it inline but align it differently */
img {
  vertical-align: middle;   /* or top / bottom */
}

Many CSS resets include img { display: block; max-width: 100%; } for this reason.


display: none — and Other Ways to Hide

display: none removes the element completely: it generates no box, takes no space, can't be clicked or focused, and is removed from the accessibility tree (screen readers ignore it). Its children are hidden too, and no child can override that.

.modal { display: none; }
.modal.open { display: block; }

There are three popular ways to hide something, and they behave differently:

display: nonevisibility: hiddenopacity: 0
Takes up spaceNoYes (an empty gap)Yes
Can be clicked / focusedNoNoYes (invisible but still there!)
Read by screen readersNoNoYes
Children can show themselvesNoYes, with visibility: visibleNo
Can be animated smoothlyNot directlySwitches at a stepYes (fades)

So: use display: none to remove something entirely, visibility: hidden to hide it while keeping its space reserved, and opacity for fade effects (combined with something that makes it non-interactive when fully hidden).

Sometimes you want text to be hidden visually but still read by screen readers (for example a label for an icon-only button). None of the three above does that; the usual solution is a "visually hidden" utility class that shrinks the element to 1px and clips it.

The hidden attribute trap

HTML has a hidden attribute, which works because the browser stylesheet contains [hidden] { display: none; }. But browser styles have the lowest priority in the cascade, so any author rule that sets display wins:

.toolbar { display: flex; }
<div class="toolbar" hidden>You should not see me</div>

In Chrome, this element was visible: its computed display was flex. A common safety rule in CSS resets fixes it:

[hidden] {
  display: none !important;
}

display: flex and display: grid (a Preview)

Block and inline layout (together called normal flow) was designed for documents. For application-style layouts — navbars, card rows, page grids — CSS has two dedicated layout systems, switched on with display:

.navbar  { display: flex; }   /* children laid out in a row (or column) */
.gallery { display: grid; }   /* children placed into rows AND columns */

The element itself stays a block on the outside; only the layout of its children changes. Their inline cousins inline-flex and inline-grid do the same but make the container sit inside a line like an inline-block. These are big topics with their own chapters: Flexbox and CSS Grid.


The Two-Value Syntax

Modern CSS lets you write the outer and inner types explicitly. The familiar one-word values are just short forms:

Short (classic)Two-value equivalent
blockblock flow
inlineinline flow
inline-blockinline flow-root
flexblock flex
inline-flexinline flex
gridblock grid
inline-gridinline grid
flow-rootblock flow-root
list-itemblock flow list-item

This table explains the whole model: inline-block really means "inline on the outside, a new independent block layout on the inside". The two-value syntax is Baseline since July 2023 (Chrome 115, Firefox 70, Safari 15), and browsers report the short form when you read the computed value — writing display: inline flex gives a computed value of inline-flex. You will still mostly see the classic one-word values in real code, and they are perfectly fine to use.


display: flow-root

flow-root creates a normal block box that starts a new block formatting context (BFC) — an independent layout area. Two practical effects:

  • The child margins don't collapse through it with the parent's margins (the "margin escaping the parent" problem from Margin & Padding).

  • It grows to contain floated children instead of letting them hang out of the bottom (you'll meet floats in Float & Clear).

.container {
  display: flow-root;   /* contain floats, stop margin collapsing with children */
}

Before flow-root existed, developers used hacks like overflow: hidden or a "clearfix" for the same effect. flow-root has been supported in all major browsers since 2020.


display: contents

display: contents makes the element's own box disappear, while its children are displayed as if they were direct children of the element's parent. Its background, border and padding are not drawn at all.

.row   { display: flex; gap: 8px; }   /* children side by side */
.group { display: contents; }        /* the wrapper "vanishes" */
<div class="row">
  <div>One</div>
  <div class="group">
    <div>Two</div>
    <div>Three</div>
  </div>
</div>

Without display: contents the flex container has only two items — One, and the group (with Two and Three stacked inside it). With it, One, Two and Three all become flex items and sit side by side in one row. This is useful when a wrapper element gets in the way of a flex or grid layout. Be careful: browsers historically had accessibility bugs with display: contents on elements like buttons, lists and tables (their semantics could be lost), and its web-features status is still listed as "limited" because of these interoperability issues. Use it on generic wrappers such as div, and test with a screen reader.


Other display Values

ValueWhat it does
list-itemA block that also gets a marker (bullet/number) through the ::marker pseudo-element, like <li>
table, table-row, table-cell, ...Make any elements behave like table parts. Was used for vertical centring before flexbox; rarely needed now
inline-tableA table that sits inline
rubyAnnotation layout for East Asian text (used by <ruby>)

Complete Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>The Display Property</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      padding: 20px;
    }
    .menu a {
      display: block;
      padding: 10px 14px;
      border-bottom: 1px solid #e5e7eb;
      color: #111827;
      text-decoration: none;
    }
    .menu a:hover {
      background: #f3f4f6;
    }
    .tag {
      display: inline-block;
      padding: 4px 10px;
      margin: 4px 2px;
      border-radius: 9999px;
      background: #dbeafe;
      font-size: 0.875rem;
    }
    .note {
      display: none;
    }
    .menu:hover + .note {
      display: block;
    }
    img {
      display: block;
      max-width: 100%;
    }
  </style>
</head>
<body>
  <nav class="menu">
    <a href="#">Operating Systems</a>
    <a href="#">Computer Networks</a>
    <a href="#">DBMS</a>
  </nav>
  <p class="note">Pick a subject to start revising.</p>
  <p>
    Topics:
    <span class="tag">Paging</span>
    <span class="tag">TCP</span>
    <span class="tag">Normalization</span>
  </p>
</body>
</html>

Result: three full-width menu rows separated by thin lines, each row clickable across its entire width (not just on the words) and turning light grey on hover. Below them, a line reading "Topics:" followed by three rounded light-blue pill tags sitting side by side in the line, with real padding. The note paragraph is hidden and takes no space until you hover over the menu (using the + combinator from Combinators & Attribute Selectors).


Common Mistakes

  • Setting width/height on an inline element (span, a) and wondering why nothing happens. Use inline-block or block.

  • Expecting vertical margin on an inline element to create space.

  • Using opacity: 0 to "hide" a button: it is still clickable and focusable.

  • Overriding the hidden attribute by setting display on the same element.

  • Building rows of boxes with inline-block and fighting whitespace gaps. Use flexbox or grid.

  • Forgetting the image baseline gap: set img { display: block; }.

  • Changing display to change meaning: a div styled to look like a button is still not a button (no keyboard support). Use the right HTML element.


Interview / Exam Questions

1. What is the difference between inline, inline-block and block?

Block elements start on a new line, fill the available width and respect width, height and all margins/padding. Inline elements flow within text, size to their content and ignore width, height and vertical margins (vertical padding is painted but doesn't affect line spacing). Inline-block elements flow within the line like inline elements but respect width, height, margins and padding like blocks.

2. Difference between display: none and visibility: hidden?

display: none removes the element from layout entirely — it takes no space. visibility: hidden makes it invisible but its space is kept. Neither is clickable nor read by screen readers; a child of a visibility: hidden parent can be made visible again with visibility: visible.

3. Why doesn't width: 200px work on a span?

A span is display: inline by default, and width/height don't apply to non-replaced inline boxes. Change it to inline-block or block.

4. Why is there a small gap between inline-block elements?

The whitespace (spaces/newlines) between the elements in the HTML is rendered as a space character, because inline-block elements are laid out like words. Its width depends on the font (about 0.25em). Using flexbox or grid avoids it.

5. Why is there extra space below an image inside a div?

Images are inline and sit on the text baseline; space is reserved below the baseline for letter descenders. Fix with display: block or vertical-align: middle/top/bottom on the image.

6. What does inline-block mean in the two-value syntax?

inline flow-root: the outer display type is inline, and the inner display type is flow-root, i.e. it establishes a new block formatting context for its contents.

7. Which of display: none, visibility: hidden and opacity: 0 keeps the element clickable?

Only opacity: 0. The element is fully transparent but still occupies space, receives clicks and focus, and is read by screen readers.


Summary

ValueKey behaviour
blockNew line, full width, all box properties work
inlineFlows in text; no width/height; no vertical margin
inline-blockFlows in text but has a real size
noneRemoved from layout and accessibility tree
flex / gridBlock outside, flex/grid layout for the children
flow-rootBlock that starts a new formatting context
contentsElement's box disappears; children take its place
  • display = outer type (block/inline) + inner type (flow, flex, grid ...).

  • It changes appearance, never HTML meaning.

  • Choose the right hiding technique: display: none, visibility: hidden or opacity: 0.

Normal flow places boxes one after another. In the next chapter, Positioning, we'll learn how to take boxes out of that flow and place them exactly where we want.