How browsers lay out pages

Topic 2 of 3 in Getting started7 min readBeginnerFree course

Overview

Before learning individual properties, it helps to know what the browser is actually doing when it lays out a page. Once you can picture the process, CSS stops feeling like trial and error.

From code to pixels

HTML becomes the DOM tree and CSS becomes the CSSOM. The browser then works out the style — which rules apply to each element — then layout — the size and position of every box — then paint — colours, text, borders and images — and finally composites the layers onto the screen. This course lives in the layout step. Changing a layout property such as width, display or margin makes the browser redo layout, paint and composite.

  1. Parse. The browser reads your HTML into the DOM (a tree of elements) and your CSS into the CSSOM.
  2. Style. For every element, it works out which rules apply and their final values — the cascade, specificity and inheritance happen here.
  3. Layout. It calculates the exact size and position of every box. This step is sometimes called reflow.
  4. Paint. It fills in pixels: text, colours, borders, shadows, images.
  5. Composite. It combines painted layers into the final image on screen.

Changing a layout property (width, display, margin, font-size…) forces the browser to repeat layout and everything after it. Changing only color skips layout; changing transform or opacity can often skip straight to compositing — which is why smooth animations use those properties.

Everything is a box

During layout, each element generates one or more boxes. (Some generate none — display: none removes the element from layout entirely.) How a box behaves depends on its display value, which has two parts:

  • an outer display type: how the box participates in its parent’s layout — as a block (takes a full line) or inline (flows with text),
  • an inner display type: how the box lays out its own children — normal flow, flex, grid and so on.
CSS
display: block;        /* outer: block,  inner: flow */
display: inline;       /* outer: inline, inner: flow */
display: flex;         /* outer: block,  inner: flex */
display: inline-flex;  /* outer: inline, inner: flex */
display: grid;         /* outer: block,  inner: grid */

You can even write both parts explicitly: display: block flex is the same as display: flex. You’ll rarely need to, but it explains why inline-flex exists — it’s a flex container that sits in a line of text.

Layout modes

The inner display type selects a layout mode — the algorithm that arranges a container’s children:

Four layout modes. Normal flow with display block: children stacked top to bottom. Flexbox with display flex: children in a row or column. Grid with display grid: rows and columns. Positioned with position absolute: an element taken out of flow and placed on top. Every element has a display value; the parent’s layout mode decides where its children go, and each child can in turn be a container for its own children.

Layout mode Turned on by Good for
Normal flow default (block, inline) documents: headings, paragraphs, lists
Flexbox display: flex rows or columns of items
Grid display: grid two-dimensional layouts
Positioned position: absolute / fixed overlays, badges, pinned elements
Table display: table (and <table>) tabular data
Multi-column columns: 3 newspaper-style text columns

The parent’s layout mode decides how its children are placed. That’s why justify-content does nothing on an element in normal flow: it’s a property for flex and grid containers. Many properties only apply in certain modes, and the browser silently ignores them elsewhere — DevTools marks such properties as inactive, as the next lesson shows.

Layout is nested

Each container lays out only its direct children. Those children can be containers too, each with its own layout mode:

HTML
<body>                       <!-- grid: header / main / footer -->
  <header>                   <!-- flex: logo and links in a row -->
    <a class="logo">…</a>
    <nav>…</nav>
  </header>
  <main>                     <!-- normal flow: article text -->
    <h1>…</h1>
    <ul class="cards">…</ul> <!-- grid: cards in columns -->
  </main>
</body>
CSS
body { display: grid; grid-template-rows: auto 1fr auto; }
header { display: flex; justify-content: space-between; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); }

Real pages combine all the layout modes, each used where it fits best. A grid item can be a flex container; a flex item can be a grid container. A display: flex on <header> has no effect on the links inside <nav> — only on the header’s own children.

Sizes flow in two directions

Layout is a conversation between parents and children:

  • Widths usually flow down. A block takes the width its parent offers; its children take widths from it.
  • Heights usually flow up. A block is as tall as its content needs; its parent grows to contain it.

That’s why height: 50% often does nothing — the parent’s height depends on its children, so a percentage of it can’t be resolved — while width: 50% works fine. You’ll see this asymmetry many times, and it explains a lot of “why doesn’t this work?” moments.

Takeaway: when something isn’t where you expect, ask two questions: what layout mode is the parent using? and what size is the parent offering? The answers explain most layout behaviour.

Exercise

One container, four layout modes

Create a .container with four child .item elements. Write four classes — .flow, .row, .grid and .overlay — that you can add to the container to switch between normal flow (items stacked), flexbox (items in a row with a 1rem gap), grid (two equal columns with a 1rem gap), and an overlay mode where the fourth item is positioned in the top-right corner of the container. Add each class in turn and observe how the same HTML is laid out differently.

HTML
<div class="container row">
  <div class="item">One</div>
  <div class="item">Two</div>
  <div class="item">Three</div>
  <div class="item badge">Four</div>
</div>

<style>
  .item { padding: 1rem; background: #eaf0fc; border: 1px solid #2d5bd7; }
  /* your code here */
</style>

Summary

  • Browsers turn HTML into the DOM and CSS into the CSSOM, then compute styles, lay out boxes, paint and composite.
  • Layout calculates the size and position of every box; changing layout properties makes the browser redo it.
  • Every element generates a box whose display value decides how it behaves and how it lays out its children.
  • The main layout modes are normal flow, flexbox, grid and positioned layout.
  • Layout is nested: each container lays out its own children, which can be containers too.