Thuta Learning
CSS
IntermediateWeb DevelopmentBeginner

Display Property

CSSLesson 1425 min

What you'll walk away with

  • Describe how block, inline and inline-block boxes treat width, height and padding
  • Explain display: flex as two types at once: block on the outside, flex on the inside
  • Choose between display: none, visibility: hidden and a screen-reader-only pattern by intent
  • Recognise how author CSS can accidentally override the hidden attribute, and prevent it

Explain it simply

Explain it simply

Picture a page as a bookshelf. Block elements are the big volumes that claim a whole shelf: each <div>, <p> or <h1> starts on a new line and takes all the width it can get. Inline elements are the words inside a sentence: <a>, <span> and <strong> flow side by side with the text and take only as much room as their content needs.

The display property is the switch that says whether a box behaves like a volume or like a word. inline-block sits in the line like a word but accepts a size like a volume. flex and grid are layout systems for the children inside the box. none removes the box from the page completely.

Problem → Why → Solution

The problem: the link ignores its height

You give the navigation links height: 40px, width: 200px and some padding so they look like buttons. In the browser, width and height do nothing at all and each link stays the size of its text. Worse, the background of the vertical padding spills over the paragraph above and does not push the content below down.

/* <a> is inline by default */
.nav a {
  width: 200px;        /* ignored */
  height: 40px;        /* ignored */
  padding: 12px 16px;  /* top/bottom paint over neighbours */
  background: #ddd;
}

Why does this happen?

An <a> is display: inline by default. Inline boxes flow inside a line box like words, so they ignore width and height, and their vertical padding is painted without making the line any taller. Nothing is wrong with the declarations; the box type is wrong. Switch the links to display: inline-block, or make their parent display: flex, and the same properties start working immediately.

Core concept: an outer type and an inner type

Every element generates a box, and display answers two questions about it. The first is the outer display type: does the box take a new line among its siblings like a block, or sit inside a line of text like an inline? The second is the inner display type: does the box arrange its own children with normal flow, or with the flex or grid algorithm?

So display: flex really means "block on the outside, flex on the inside", and display: inline-flex means "inline on the outside, flex on the inside". Modern browsers also accept the two-keyword spelling, such as display: inline flex, which makes this split explicit.

ValueWhat it does
blockStarts a new line, fills the width, honours width, height and all margins
inlineFlows with the text, ignores width and height
inline-blockSits in the line but honours width, height and padding fully
flex / gridA block box that lays out its children with flex or grid
noneGenerates no box at all: no space, no paint, gone from the accessibility tree

On inline boxes, left and right padding and margins work, but vertical margins have no effect on layout. Vertical padding is painted as background yet does not raise the line, so it overlaps neighbouring lines. When inline-block boxes are separated by a space or newline in the HTML, that whitespace renders like a text space, about 4px at a 16px font size. That gap is one reason larger layouts moved from inline-block to flex or grid.

Changing an element's default display does not change its meaning. An <a> set to display: block is still a link, and a <ul> set to display: flex is still announced as a list by screen readers. Display controls appearance only.

Read display as two questions
  1. Does it generate a box? (none = no)
  2. Outside: block or inline?
  3. Inside: flow, flex or grid?
  4. Only then check sizing properties

When width or height does nothing, check the box's outer type before touching the property.

General pattern

css
selector {
  display: block | inline | inline-block;  /* outer behaviour */
}

container {
  display: flex | grid;  /* block outside, flex/grid inside */
}

selector {
  display: none;  /* no box, no space, not announced */
}

The first rule changes how the box itself sits among its neighbours. The second picks the algorithm that arranges the box's children. none stops the box from being generated at all.

Example: nav links and a card list

This example changes display in two places. .nav-link changes the outer type of the box itself, while .card-list changes the inner type that arranges its child cards. Assume the HTML has <a class="nav-link"> elements and a <div class="card-list"> holding cards.

.nav-link {
  display: inline-block;
  padding: 10px 14px;
}

.card-list {
  display: flex;
  gap: 16px;
}

Code breakdown

  1. Line 2display: inline-block;

    The links still sit side by side in the line, but each is now a full box that accepts sizing properties.

  2. Line 3padding: 10px 14px;

    The 10px top and bottom now really make the line taller and enlarge the click area. While the link was inline, the same 10px was only painted and overlapped nearby lines.

  3. Line 6.card-list {

    This rule does not touch the cards themselves; it changes the parent that holds them.

  4. Line 7display: flex;

    Outside, the list still takes a full line as a block. Inside, every child card becomes a flex item and they line up in one row.

  5. Line 8gap: 16px;

    It adds 16px only between cards, never before the first or after the last, and there is no unpredictable inline-block whitespace gap.

You should see
Navigation links will be able to have padding set on them. The card list becomes a flex layout with gaps between the cards.

Hiding an element: three different intents

"Hide it" can mean three different things. Do you want the element gone for everyone, invisible but still holding its space, or invisible to the eye while screen-reader users still hear it? Note that visibility: hidden also hides the element from screen readers; hiding it from the eye only needs the visually-hidden (sr-only) pattern.

TechniqueSpaceScreen reader
display: noneRemovedNot announced
visibility: hiddenKeptNot announced
.sr-only patternAbout 1pxAnnounced
sr-only.css
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

In real projects

Where this shows up in real projects

Design systems usually build small components that must live inside text, such as badges, tags and pills, with inline-block or inline-flex, so height and padding are exact while the component still sits in the line. Buttons with an icon and a label commonly use inline-flex, because it makes centring the icon against the text easy.

In disclosure UIs such as accordions, dropdown menus and modals, hiding a closed panel with display: none (or the hidden attribute) is correct, because links inside it must not be reachable with the Tab key. Icon-only buttons add a "Close" label with .sr-only so screen-reader users hear what the button does. Responsive layouts constantly switch a mobile menu between display: none and display: flex inside media queries.

Common mistakes

Common mistakes

1. Sizing an inline element and expecting it to work

Giving <span class="badge"> height: 24px and seeing no change looks like a browser bug. Check display in the DevTools Computed panel first: if it says inline, switch to inline-block or inline-flex.

2. Author CSS overriding the hidden attribute

The hidden attribute is just a rule in the browser's default stylesheet, [hidden] { display: none }. Your own .panel { display: flex } beats it, so <div class="panel" hidden> stays visible. Write .panel:not([hidden]) { display: flex }, or add [hidden] { display: none !important; } to your global reset.

3. Hiding screen-reader text with visibility: hidden

visibility: hidden keeps the space but also removes the element from the accessibility tree, so a label meant to be read aloud is heard by nobody. Use the .sr-only pattern for that kind of text.

Best practices

Best practices

  • Before hiding anything, ask who it should be hidden from, then choose display: none, visibility: hidden or .sr-only.
  • For layout, give the parent flex or grid with gap instead of fighting inline-block whitespace.
  • Put [hidden] { display: none !important; } in your global reset so component CSS cannot silently cancel the hidden attribute.

Mini challenge

Mini challenge: fix a product card

Use the right display values to fix the tags, buttons and detail panel of the product card below.

  • .tag elements stay side by side in the line and are exactly 24px tall
  • The two buttons in .actions sit in one row with a 12px gap
  • The .details panel stays hidden while it has the hidden attribute, and is a grid when shown
  • The "(on sale)" text is invisible to the eye but still read by screen readers
card.html
<style>
  .tag {
    /* TODO: stay in the line, but accept height: 24px */
  }
  .actions {
    /* TODO: one row of buttons, 12px apart */
  }
  /* TODO: show .details as a grid ONLY when it is not [hidden] */
  .sr-only {
    /* TODO: visually hidden, still announced */
  }
</style>

<article class="card">
  <h2>Trail Shoes <span class="sr-only">(on sale)</span></h2>
  <p><span class="tag">New</span> <span class="tag">Waterproof</span></p>
  <div class="actions"><button>Add to cart</button><button>Details</button></div>
  <section class="details" hidden>Size chart and materials</section>
</article>

Expected: Both tags are 24px tall and side by side, and the buttons are 12px apart. The details panel is not rendered and takes no space. "(on sale)" is invisible but still exists in DevTools as a 1px by 1px box.

Show hint

For the tags, pair inline-block with line-height: 24px so the text is centred too. For the panel, use the :not([hidden]) selector.

Show solution
card.html
<style>
  .tag {
    display: inline-block;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    border-radius: 12px;
    background: #e0f2fe;
  }
  .actions {
    display: flex;
    gap: 12px;
  }
  .details:not([hidden]) {
    display: grid;
    gap: 8px;
  }
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
</style>

<article class="card">
  <h2>Trail Shoes <span class="sr-only">(on sale)</span></h2>
  <p><span class="tag">New</span> <span class="tag">Waterproof</span></p>
  <div class="actions"><button>Add to cart</button><button>Details</button></div>
  <section class="details" hidden>Size chart and materials</section>
</article>

Quick quiz 1 / 3

A <span> gets width: 120px; height: 40px; and its display is left alone. What happens?

Quick quiz 2 / 3

Which technique suits a label that screen readers must announce but sighted users should not see?

Quick quiz 3 / 3

The CSS has .menu { display: flex; } and the HTML is <nav class="menu" hidden>. What happens?

What to remember

  • display sets two things at once: the outer type (block or inline) and the inner type (flow, flex or grid).
  • Inline boxes ignore width and height; inline-block stays in the line and accepts a size.
  • display: none removes the space and the accessibility node; visibility: hidden keeps the space but also hides it from screen readers.
  • Author CSS can override the hidden attribute, so guard it with :not([hidden]) or a !important reset.

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Display Property | Thuta Learning