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.
What you'll take away
- Follow HTML and CSS into the DOM and CSSOM, then style, layout, paint and composite
- Pick
defer,asyncor 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.
HTML → DOMRead HTML into a tree
Nothing on screen yet
Nothing on screen yet
100 layouts · Reading offsetWidth after writing a style, every time round, makes the browser recalculate right then, every time · this frame takes 44.0 ms (simulated): the frame drops
The numbers are a model of a busy page, not real measurements. The thrash.ts code below was measured in real Chrome: 99 layouts vs 0 during the loop.
You want a box to slide to the right, smoothly. Which value should you change?
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.
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.
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)
| Change | Runs again |
|---|---|
| width, height, top, margin, font-size | layout → paint → composite |
| color, background, box-shadow | paint → 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
<!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>
open in a browser (checked in headless Chrome: scripts.expect.json)It printed: plain (sees 1 paragraph) → defer → module → DOMContentLoaded · async before loadOpened 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.
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.
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.