How a web page draws itself

From the bytes of HTML to pixels on screen: DOM, CSSOM, layout, paint and composite, one step at a time. Then change a property to see which stages run again, and why transform is smoother than width.

Level: IntermediateWatch: 11 steps · ~1 minuteChange values, watch the frame budgetRecall: 3 questions · 8 cardsCompare 5 properties · 4 ways to load a scriptHTML · CSS · TypeScript · Tailwind

What you'll take away

  • Follow HTML and CSS into the DOM and CSSOM, then style, layout, paint and composite
  • Pick defer, async or a module so a script doesn't stop the HTML being parsed
  • Know why width runs layout again, color only repaints and transform only composites
  • Spot layout thrashing, and keep a frame inside 16.7 ms by reading first, then writing

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
HTML → DOMstage 1 of 6
DOM
CSSOM
Style
Layout
Paint
Composite

HTML → DOMRead HTML into a tree

htmlheadbodylinkh1pimgbuttondiv.ad
shop.example

Nothing on screen yet

Nothing on screen yet

Reading the HTML → …
Step 1 / 11 · step by step
0DOM elements
0CSSOM rules
emptyon screen
–this frame (simulated)
16.7 msone frame at 60 Hz

The picture: six workshops along a street, one for each stage, lit while that stage runs. The billboard above them shows what the stage makes (the tree, the rules, the boxes, the layers) and, on the right, the browser's screen, which stays empty until composite puts the first frame there. Which stages run again is what's typical in Chromium and WebKit (each engine can differ in the details). Every time on this page is a model of a busy page, not a real measurement, except the numbers in the code, which were measured in Chrome.

Step 4 · Every option at once, on one clock

Compare them all: How a page draws itself

The same box making the same move on a busy page, each tile changing a different property every frame. Under each picture are the frames of half a second.

0.0 s
widthstyle → layout → paint → composite30 fps · 60 of 120 dropped in 2 s · 20 ms per frame

Use it when A one-off change when the layout really changes (not every frame)

Skip it when Animating every frame: layout and paint run again each time

The stages that run again are what's typical in Chromium / WebKit (engines can differ). Each stage's time is a model, not a real measurement. Press any tile to see it large above, with when it fits.

From HTML to a picture

  • Parse: reads HTML into the DOM and CSS into the CSSOM, piece by piece as they arrive
  • Style: matches the CSS rules to the elements that will show
  • Layout: works out the size and position of every box
  • Paint: draws colors, text, shadows and images as pixels on layers
  • Composite: stacks the layers into the picture on screen (usually on the GPU)

What you change, and what runs again (typically)

ChangeRuns again
width, height, top, margin, font-sizelayout → paint → composite
color, background, box-shadowpaint → composite
transform, opacity (on its own layer)composite only

What blocks rendering

CSS in <head> blocks the first paint (otherwise the page would flash). A plain <script> blocks HTML parsing. Use defer or type="module" for the page's own scripts, and async for scripts that depend on nothing else.

One frame

At 60 Hz, everything in a frame (JavaScript, style, layout, paint, composite) has to finish in ~16.7 ms. That's why animating transform / opacity is smooth, and why you shouldn't read layout values in between style writes inside a loop.

In real code

scripts.html
<!doctype html>
<html lang="th">
<head>
  <meta charset="utf-8">
  <script>
    window.order = [];
    document.addEventListener("DOMContentLoaded", () => order.push("DOMContentLoaded"));
    addEventListener("load", () => order.push("load"));
  </script>
  <!-- defer: download now, run after the HTML is parsed, in document order, before DOMContentLoaded. -->
  <script src="data:text/javascript,order.push('defer')" defer></script>
  <!-- type="module" is deferred by default. -->
  <script type="module">order.push("module")</script>
  <!-- async: download now, run as soon as it arrives: the order against the rest is not fixed. -->
  <script src="data:text/javascript,order.push('async')" async></script>
</head>
<body>
  <p>ก่อน script</p>
  <!-- A plain script stops the parser: it sees only what is above it. -->
  <script>order.push(`ปกติ (เห็น ${document.querySelectorAll("p").length} ย่อหน้า)`)</script>
  <p>หลัง script</p>
</body>
</html>
Run: open in a browser (checked in headless Chrome: scripts.expect.json)It printed: plain (sees 1 paragraph) → defer → module → DOMContentLoaded · async before load

Opened in headless Chrome to check the real order: a plain script only sees the paragraph above it. defer and module run after the HTML is parsed, in order, before DOMContentLoaded. async only promises to run before load.

Common questions

Why is moving with transform smoother than changing width?

Change width every frame and the browser lays out and paints again each time, which easily blows 16.7 ms on a busy page. A transform on its own layer skips straight to composite, so it makes every frame.

Table: what runs again

What's the difference between async and defer?

Both download while the HTML is parsed. async runs the moment it arrives, in no fixed order. defer waits until parsing is done, then runs in order before DOMContentLoaded. Most of a page's own scripts want defer; async suits things like analytics.

Why is CSS render-blocking?

If the browser drew before it had every rule, the page would flash up unstyled and then redraw. So it waits for the CSS in <head> before the first paint. Small CSS that arrives fast means a page you see fast.

What is layout thrashing?

Writing a style, then reading something like offsetWidth, over and over in a loop. Every read makes the browser lay out again right then to give a correct number, so 100 boxes means about 100 layouts. Read everything first, then write.

Reflow vs repaint: what's the difference?

A reflow is another layout: it happens when sizes or positions change (width, margin, font-size), and paint and composite follow. A repaint is when only the look changes (color, background, box-shadow): layout is skipped and it just draws again. So reflow costs more.

Table: what runs again

Can I just put will-change on everything?

will-change: transform tells the browser ahead of time to give that box its own layer, so moving it needs no new paint. But every layer costs GPU memory, so put it only on things that will really move.

Where does 16.7 ms come from?

A 60 Hz screen shows a new picture 60 times a second, and 1000 ÷ 60 is about 16.7 ms a frame. JavaScript, style, layout, paint and composite all have to fit in it. Miss it and the frame waits for the next one, which you see as stutter. At 120 Hz you get about 8.3 ms.

Next lesson

Flexbox vs GridLevel: Basic

Layout works out each box's size and place; next, Flexbox and Grid, the way you tell it where the boxes should go.