Skip to article content
FrontendXLesson 17 / 30

Display types and formatting contexts

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

intermediate · about 35 minutes · 2 exercises

  • Predict
  • Implement

What you will be able to do

#
  • Distinguish block, inline and inline-block behavior.
  • Explain what a block formatting context contains.

Overview

#

The display property decides how a box participates in layout. block boxes start on a new line and fill the inline size. inline boxes flow with text, ignore width and height, and only respect horizontal margins and padding. inline-block flows with text like inline but accepts width, height and vertical spacing.

snippet.css
.label { display: inline; }
.chip { display: inline-block; padding: 0.25rem 0.5rem; border-radius: 999px; }
.panel { display: block; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

A block formatting context is a region where floats and margins are contained. An element establishes one when it is a flex or grid container, when it has overflow other than visible, or when it is a float or display: flow-root. This matters because floats inside a normal block can escape and overlap following content. display: flow-root is the modern, intention-revealing way to contain them.

snippet.css
.clearfix { display: flow-root; }

The visually-hidden pattern above removes content visually while keeping it available to assistive technology. It is the correct way to add off-screen text that screen reader users hear but sighted users do not see, and it is different from display: none, which removes content from the accessibility tree entirely. Choose the technique by whether the content should be perceived by everyone or only by assistive technology.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What happens when you set width: 200px and height: 50px on a display: inline element?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.css
    span { display: inline; width: 200px; height: 50px; }
    Choose the best answer
  2. Exercise 2Implement

    Write a utility class that hides content visually but keeps it available to screen readers.

    starter.css
    .visually-hidden { /* add properties */ }

    Reveal the reference answer
    reference.css
    .visually-hidden {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      padding: 0;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
      border: 0;
    }

    Authored expected output: A class that removes content from view without removing it from the accessibility tree.

    Clipping to a 1px box keeps the content in the tree and in the tab order while making it invisible.

Common misconceptions

#
  • inline elements respect width and height.
  • display:none and visually-hidden are interchangeable.

Follow-up practice

#
  • html-css-18-flexbox

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.