Skip to main content

CSS Layout

Normal Flow

Written by Updated Normal Flow exercises

Understand block flow, inline flow, formatting contexts, containing blocks, and why positioned elements use particular reference boxes. The lesson explains block and inline flow, containing blocks, formatting contexts, margin behavior, and the reference used by positioned descendants.

Before you add Flexbox, Grid or positioning, the browser already lays out every page in a default way: blocks stack from top to bottom, and inline text flows along each line, wrapping at the end. That default is called normal flow.

Understanding it saves a lot of guesswork. Most layout "bugs" happen because an element is not in the layout mode you think it is in. The individual modes are covered in CSS Display and CSS Position.

The two rules of normal flow

  • Block boxes, like paragraphs, headings and divs, stack one below the other and take the full width.
  • Inline content, like text, links and images, flows along a line from left to right and wraps onto the next line when it runs out of space.

Flexbox, Grid, floats and positioning change these rules for particular elements. Everything else still follows normal flow, and it is what pushes later content down the page as earlier content grows.

"Relative to what?" The containing block

Example

css

.card {
  position: relative;
}

.card__badge {
  position: absolute;
  inset-block-start: 0.75rem;
  inset-inline-end: 0.75rem;
}

An absolutely positioned element is placed relative to its containing block: the nearest ancestor that is positioned. Here the card is position: relative, so the badge sits in the card's top-right corner instead of the page's. Transforms and a few other properties also create a containing block, which can surprise you.

Contain floats with flow-root

Example

css

.article {
  display: flow-root;
}

display: flow-root makes the element a self-contained layout area, called a new block formatting context. Floated images inside it no longer stick out of the bottom, and its children's margins no longer leak out through its edges. It replaces old tricks like overflow: hidden and clearfix hacks.

Flow Display Values

ValueFormatting behaviorExample
blockCreates a block-level flow box.display: block
inlineParticipates in an inline formatting context.display: inline
inline-blockFlows inline while accepting block dimensions.display: inline-block
flow-rootCreates a new block formatting context.display: flow-root

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Normal Flow example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.card {
  position: relative;
}

.card__badge {
  position: absolute;
  inset-block-start: 0.75rem;
  inset-inline-end: 0.75rem;
}

.article {
  display: flow-root;
}
  </style>
</head>
<body>
  <main class="article"><article class="card"><span class="card__badge">New</span><h1>Containing block</h1><p>The badge is positioned against this card.</p></article></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
display: flow-root58795313

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • If two vertical margins in normal flow meet, they collapse into one, the larger of the two. Padding or a border between them stops this.
  • Lean on normal flow. A page that simply stacks its content needs no layout CSS at all, and it adapts to any screen.

Normal Flow exercises

Inline to block

Make the span a block, so it starts on its own line.

The HTML your code works on
<p class="text">One <span class="word">two</span> three</p>
<div class="block">Block</div>
<div class="gone">Gone</div>

Result

Check your answer to see it here.