How does the browser render a page? (including reflow vs repaint)
MediumBrowserRenderingPerformance
What happens when the browser renders a page? What's the difference between reflow and repaint?
TL;DR
The critical rendering path is: parse HTML into the DOM, parse CSS into the CSSOM, combine them into the render tree, compute geometry in layout, paint pixels, and composite layers on the GPU. Reflow (layout) recalculates sizes and positions and is the expensive one. Repaint redraws pixels without changing geometry (a color change). Changes to transform and opacity can skip both and only re-composite.
Key points
- CSS blocks rendering: the browser won't paint until it has the CSSOM.
- Synchronous
<script>blocks parsing. Usedefer(runs in order after parsing) orasync(runs as soon as it loads) instead. - Changing
width,height,top, font size or adding DOM nodes causes reflow → repaint → composite. - Changing
colororbackgroundcauses repaint → composite. transformandopacityonly need composite, which is why animations should use them.- Layout thrashing: reading a layout value (
offsetHeight) right after a write forces a synchronous reflow. Batch reads, then writes.
Example
js
// Bad: forces a reflow on every iteration
for (const el of items) {
el.style.width = box.offsetWidth + 'px'
}
// Good: read once, then write
const width = box.offsetWidth
for (const el of items) el.style.width = width + 'px'css
/* Animate with compositor-only properties */
.drawer {
transform: translateX(-100%);
transition: transform 200ms;
}
.drawer.open {
transform: translateX(0);
}Common follow-ups
- How do you speed up first render? Inline critical CSS,
deferscripts, preload key fonts and the LCP image, and reduce render-blocking resources. - What does
will-changedo? Hints that an element will animate so the browser can promote it to its own layer. Overusing it wastes memory.
What interviewers look for
Tip
Walk the pipeline in order, then show you know which CSS changes trigger which stages.
- Red flag: animating
left/toporwidthinstead oftransform.