← All lessons
How a browser renders a page
From text to pixels
Files
Trees
Layout
Screen
render.js
const dom = parseHTML(html);const cssom = parseCSS(css);const tree = renderTree(dom, cssom); // drops display: noneconst boxes = layout(tree, viewport.width);const records = paint(boxes);gpu.composite(layers(records));app.css
body { margin: 0; font: 16px/24px sans-serif; }h1 { font-size: 32px; line-height: 40px; margin: 0 0 8px; }.note { margin: 0; color: gray; }.ad { display: none; }Change one property and see which stages run again
In a 1 s animation at 60 fps
Style
Layout
Paint
Composite
The box has will-change: transform, so it has its own layer and the GPU can move it without a repaint
change.js
// size or place: layout, paint, compositebox.style.width = "160px";box.style.top = "24px";// only pixels: paint, compositebox.style.color = "orange";// only the layer: compositebox.style.transform = "translateX(48px)";box.style.opacity = "0.4";box.css
.box { position: relative; will-change: transform; /* its own layer, so transform skips paint */}Layout thrashing read, write, read, write
Last run
thrash.js
// each read finds a dirty layout and forces onefor (const bar of bars) { const w = bar.offsetWidth; bar.style.width = next(w) + "px";}batch.js
// read everything first: one layoutconst widths = bars.map((b) => b.offsetWidth);// then write: layout waits for the framebars.forEach((b, i) => { b.style.width = next(widths[i]) + "px";});Topics
Next: How service workers work
Open lesson