Part 01 · How the Browser Works
Layout, Paint and Composite
The three steps after the render tree: computing boxes, filling pixels and stacking layers on the GPU, and what each one costs.
⏱ 6 min · Station 4 of 125
The problem
The designer wants ShopLite's "deal of the day" cards to slide in from the left. The first version animates left. It's smooth on the designer's MacBook and stutters on a Redmi phone. A teammate changes one line to animate transform instead, and it's smooth everywhere.
Same visual result, very different cost. The difference is which steps of the browser's pixel pipeline each change forces it to redo, on every single frame.
The idea
After the render tree is built, every frame goes through the pixel pipeline:
- Style: which CSS rules apply to which element, and their final values.
- Layout: the size and position of every box (also called reflow).
- Paint: recording what to draw for each element: text, colours, borders, images.
- Composite: splitting the page into layers, rasterising them, and having the GPU stack them into the final image.
A change only reruns the pipeline from the first step it affects. Geometry changes start at layout, colour changes start at paint, and transform/opacity changes can skip straight to composite.
How it works
- Style. When a class, an inline style or the DOM changes, the browser marks elements dirty and recomputes their styles. Cost grows with the number of elements and the complexity of selectors.
- Layout. Boxes depend on each other: a card's width depends on the grid, its height on its text. So changing
left,width,height,margin,topor font size can move other boxes too, and the browser recomputes the affected part of the tree. - Pre-paint and paint. The browser walks the laid-out tree, works out which layer each element belongs to, and records display lists (drawing commands, not pixels yet). Changing
color,backgroundorbox-shadowreruns this for the affected elements. - Raster. Display lists become pixels, in tiles, on raster threads (often on the GPU), not on the main thread.
- Composite. The compositor thread positions the layers and tells the GPU to draw the frame. An element with its own layer can be moved (
transform), faded (opacity) or scrolled just by changing how that layer is placed: no style, no layout, no paint. - The main thread isn't needed for composite-only animations. A CSS
transformanimation is handed to the compositor thread, so it keeps running smoothly even while JavaScript is busy on the main thread.
60 cards move back and forth for 2 seconds, CPU 4x slower. Which version keeps the main thread busiest?
Visual
What each change costs
| You change | Style | Layout | Paint | Composite | Examples |
|---|---|---|---|---|---|
| geometry | yes | yes | yes | yes | left, top, width, height, margin, padding, font-size |
| appearance | yes | no | yes | yes | color, background, box-shadow, border-radius |
| transform / opacity | yes (JS) or no (CSS anim) | no | no | yes | transform: translate/scale/rotate, opacity |
| scroll | no | no | no | yes | scrolling a layer the compositor owns |
Layers aren't free: each one costs GPU memory (a 400 x 300 layer at 2x density is about 1.9 MB). will-change: transform creates one up front; put it on the few things that animate, not on everything.
See it live
Measured on an Intel i3-10110U laptop (4 threads), 7 GB RAM, Ubuntu 26.04, Chrome 151 headless via Playwright 1.64.1, CPU throttled 4x, the lab in Docker on the same machine.
animate.html draws 60 product cards and moves or recolours them, either from JavaScript (one requestAnimationFrame loop that sets a style on every card) or with a CSS animation:
<!doctype html>
<html>
<head>
<title>layout, paint, composite</title>
<style>
body { margin: 0; font-family: system-ui; }
.card { position: relative; left: 0; width: 160px; height: 40px; margin: 4px; background: #ffcf70; border-radius: 6px; }
/* ?mode=left moves the cards by changing `left`: layout + paint every frame
?mode=color changes the background colour: paint every frame
?mode=transform moves them with `transform`: composite only */
@keyframes left { from { left: 0 } to { left: 400px } }
@keyframes color { from { background: #ffcf70 } to { background: #7cc4ff } }
@keyframes transform { from { transform: translateX(0) } to { transform: translateX(400px) } }
/* for the practice */
@keyframes opacity { from { opacity: 1 } to { opacity: 0.3 } }
@keyframes blur { from { transform: translateX(0); filter: blur(0) } to { transform: translateX(400px); filter: blur(1px) } }
</style>
</head>
<body>
<script>
const q = new URLSearchParams(location.search);
const mode = q.get("mode") ?? "left";
const cards = [];
for (let i = 0; i < 60; i++) {
const d = document.createElement("div");
d.className = "card";
d.textContent = "Product " + (i + 1);
// ?driver=css: a CSS animation. ?driver=js: JavaScript sets the property every frame.
if (q.get("driver") !== "js") d.style.animation = `${mode} 1s linear infinite alternate`;
document.body.append(d);
cards.push(d);
}
if (q.get("driver") === "js") {
const set = {
left: (d, p) => (d.style.left = p * 400 + "px"),
color: (d, p) => (d.style.background = `hsl(${40 + p * 160} 90% 70%)`),
transform: (d, p) => (d.style.transform = `translateX(${p * 400}px)`),
}[mode];
const tick = (now) => {
const p = Math.abs(((now / 1000) % 2) - 1);
for (const d of cards) set(d, p);
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
}
</script>
</body>
</html>
tools/animate.mjs records a 2-second performance trace for each version and adds up the main-thread events:
// node animate.mjs: 60 cards animated three ways for 2 seconds, CPU throttled 4x,
// first by JavaScript (a property set every frame), then by a CSS animation.
// Which pipeline steps does the main thread run for each?
import { open, trace, mainThreadSummary, WEB, ms } from "./lib.mjs";
const steps = ["UpdateLayoutTree", "Layout", "PrePaint", "Paint", "Layerize"];
for (const driver of ["js", "css"]) {
console.log(`\n${driver === "js" ? "JavaScript sets" : "CSS animation of"} style layout pre-paint paint main thread busy`);
for (const mode of ["left", "color", "transform"]) {
const { browser, page } = await open({ cpu: 4 });
await page.goto(`${WEB}/x/animate.html?mode=${mode}&driver=${driver}`, { waitUntil: "load" });
await page.waitForTimeout(500);
const events = await trace(browser, page, () => page.waitForTimeout(2000));
await browser.close();
const s = mainThreadSummary(events, [...steps, "RunTask"]);
const cell = (k) => `${String(s[k].count).padStart(3)}x ${ms(s[k].ms).padStart(7)}`.padEnd(14);
console.log(`${mode.padEnd(17)} ${cell("UpdateLayoutTree")}${cell("Layout")}${cell("PrePaint")}${cell("Paint")}${ms(s.RunTask.ms).padStart(6)} of 2000 ms`);
}
}
JavaScript sets style layout pre-paint paint main thread busy
left 120x 71 ms 121x 39 ms 121x 57 ms 7381x 355 ms 679 ms of 2000 ms
color 120x 65 ms 0x 0 ms 121x 16 ms 7381x 290 ms 449 ms of 2000 ms
transform 120x 75 ms 0x 0 ms 120x 21 ms 0x 0 ms 306 ms of 2000 ms
CSS animation of style layout pre-paint paint main thread busy
left 121x 162 ms 121x 53 ms 121x 59 ms 7381x 348 ms 820 ms of 2000 ms
color 0x 0 ms 0x 0 ms 0x 0 ms 0x 0 ms 4 ms of 2000 ms
transform 0x 0 ms 0x 0 ms 0x 0 ms 0x 0 ms 5 ms of 2000 ms
Reading it (about 120 frames in 2 s, so "120x" means once per frame):
- JS sets
left: every step runs every frame. Paint runs 7,381 times: once per card per frame. 679 ms of the 2 seconds the main thread was busy. - JS sets
color: layout drops to 0. Paint still runs for every card: 449 ms busy. - JS sets
transform: layout and paint drop to 0. What's left is the JavaScript itself plus style recalculation: 306 ms. - CSS animation of
transform: 5 ms of main-thread time in 2 seconds. The compositor thread does all of it. - CSS animation of
leftis the worst (820 ms): a CSS animation doesn't help when the property itself needs layout. - CSS animation of
background-coloralso showed no main-thread work in this Chrome build: Chrome ran it off the main thread too. Don't count on that everywhere;transformandopacityare the two properties every major browser animates on the compositor.
| Animating | Main thread busy (of 2000 ms) | vs left |
|---|---|---|
left (JS) | 679 ms | |
color (JS) | 449 ms | -34% |
transform (JS) | 306 ms | -55% |
transform (CSS) | 5 ms | -99% |
In real apps
- Carousels, drawers, toasts and skeleton shimmers move with
transformand fade withopacity. The Web Animations API and libraries like Framer Motion and GSAP push you toward these properties. - Expanding an accordion truly changes height, so teams use the FLIP technique: measure the end state once, then animate a
transform: scalethat fakes the change, and set the real height at the end. - Product image zoom on hover uses
transform: scale, not a biggerwidth. - Sticky headers and parallax are built so the compositor can move them during scrolling without asking the main thread.
What goes wrong in production
- Animating
height,toporbox-shadowon many elements: fine on a laptop, janky on a 4x slower phone. will-changeon everything: hundreds of layers, GPU memory blows up, and on low-end phones the browser starts dropping layers or the tab gets killed.- A composite-only animation that suddenly isn't: adding
filter: blur()or changingwidthin the same keyframes pulls the whole animation back onto the main thread. - Big paint areas: a full-screen gradient or a huge
box-shadowthat repaints every frame can cost more than layout.
Key takeaway
Every change reruns the pipeline from the first step it touches: geometry from layout, colours from paint, transform and opacity only at composite. Animate transform and opacity, preferably as CSS animations, and keep layout and paint out of every-frame work.
Practice
animate.html has two more keyframes for you: opacity (fade from 1 to 0.3) and blur (the same transform move, plus filter: blur() going from 0 to 1px). Predict the main-thread time of each as a CSS animation, then measure with ?mode=opacity&driver=css and ?mode=blur&driver=css (add them to the list in animate.mjs).
Answer
opacity behaves like transform: the compositor fades the layer. We measured 7 ms of main-thread time in 2 seconds, with no style, layout or paint events.
blur is the surprise. Adding one filter to the same transform keyframes pulled the whole animation back onto the main thread: style recalculation ran 121 times (262 ms) and the main thread was busy for 777 ms, about as bad as animating left. One extra property in a keyframe can cancel the compositor path, so check the trace instead of trusting a rule. Browser versions change these details too (see the background-color row above).
Station stamp
Finish the chapter
Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.
Go Pro to earn XP →