Browser Rendering
How a browser turns HTML and CSS into the pixels you see on screen.
What is it?
With the HTML and CSS in hand, the browser still has to work out what each element is, how it should look, and where it goes, then draw it. This is called rendering. It happens in roughly four stages:
- DOM: the browser reads the HTML and builds the live tree of elements.
- Render tree: at the same time it works out which CSS rules apply to which elements. Combining the two gives a tree of only the visible elements and their styles.
- Layout: it calculates the exact size and position of every visible element, in pixels.
- Paint: it fills in the pixels (colors, text, images, borders) according to that layout, and the page appears.
When something changes later (new content, modified CSS, a resized window), the browser may redo some or all of these steps.
Explain like I'm 10
It's like building a stage set. You decide which props will be used (render tree), measure where each one goes (layout), then stagehands place and paint everything (paint) so the audience sees the finished scene.
Examples
The stages, named
HTML ──┐
├──> Render Tree ──> Layout ──> Paint ──> Pixels on screen
CSS ──┘HTML and CSS combine into a render tree of visible elements and their styles. The browser then computes sizes and positions (layout) and fills in the pixels (paint).
A change that triggers re-rendering
// This changes an element's width — the browser
// must recalculate layout for anything nearby,
// then repaint the affected area.
box.style.width = "400px";Some changes cost more than others. Changing size or position (like width) makes the browser redo layout. A purely visual change (like color) can sometimes skip straight to repainting.
How it works
The browser doesn't wait for the whole HTML file to download. It parses HTML as it streams in and builds the DOM bit by bit. CSS is parsed the same way, and once enough of both has arrived, the browser starts the render tree and layout.
Elements hidden with CSS like display: none are left out of the render tree entirely, since they never need layout or paint.
Parse HTML --> DOM tree \
>--> Render Tree --> Layout --> Paint
Parse CSS --> Style rules /Why does it exist?
Separate stages let the browser redo only what a change affects. If only colors change, it doesn't recompute every element's size and position. If one element's size changes, it doesn't repaint the whole screen. That's what keeps pages responsive while content keeps updating.
When to use it
This matters once you care why a page feels slow or janky, especially when animating or updating it often. Knowing that some changes are cheap (paint only) and others are expensive (they trigger layout) shapes how you write CSS and JavaScript.
When not to use it
For a mostly static page of HTML and CSS, the browser handles the pipeline for you. This model mainly pays off during performance work.
Common mistakes
Assuming every CSS change is equally cheap, when changes to size or position force a costlier layout recalculation.
Thinking the whole page re-renders from scratch on every small update, when the browser recomputes only what changed.
Forgetting that display: none elements are skipped entirely during rendering, unlike elements made invisible in other ways.
Practice exercises
- Easy:
In your own words, list the four main stages of the rendering pipeline in order.
- Medium:
Explain why changing an element's color is usually cheaper for the browser than changing its width.
- Hard:
In your browser's developer tools performance panel, record a page interaction and find a 'layout' or 'paint' step in the timeline.
Interview questions
What are the main stages of browser rendering?
Parse HTML and CSS into a render tree of the visible elements and their matched styles. Compute layout, the exact size and position of each visible element. Then paint the pixels: colors, text, images, and borders.
What is the render tree?
The DOM combined with the CSS rules that match each node, containing only elements that will actually be drawn. Elements like those set to display: none get no node in it at all.
Why does changing an element's width potentially cost more than changing its color?
Width changes the element's size, which can move and resize its neighbors too, so the browser must redo layout for that part of the page before repainting. Color only affects painting, not geometry, so the browser can often skip layout and go straight to repainting.
What's the difference between 'layout' (also called reflow) and 'paint' (also called repaint)?
Layout calculates the exact size and position, in pixels, of every visible element. Paint comes after and fills in the pixels (colors, text, borders, images) using that geometry. A change can trigger both, only paint, or sometimes neither.
What is compositing, and how does it let some changes skip both layout and paint?
The browser can put some elements on separate layers that the GPU can move, scale, or fade on their own. If a change only affects one of these layers, like sliding it or changing its transparency, the browser just recombines the layers on the GPU, with no layout or repaint.
Why are `transform` and `opacity` recommended for animations over animating `top`/`left` or `width`/`height`?
transform and opacity can usually be handled entirely by compositing, skipping layout and paint. Animating top/left or width/height changes the element's geometry, so layout (and usually paint) reruns on every frame. That's far more expensive and more likely to drop frames.
What's the difference between `display: none` and `visibility: hidden`, in terms of rendering?
display: none removes the element from the render tree: no box, no space, no layout or paint. visibility: hidden keeps it in the render tree and layout still reserves its space; it just isn't painted, so neighbors don't shift into the gap.
How is `opacity: 0` different from `visibility: hidden`, even though both make something invisible?
Neither is visible, but an opacity: 0 element is still fully interactive: it can be clicked, focused, and tabbed to. A visibility: hidden element is removed from the accessibility tree and can't be focused or clicked. Both still take up their layout space.
What is the Critical Rendering Path, and why does it matter for perceived load speed?
It's the steps that must finish before any pixels appear: parse HTML into a DOM, parse CSS into matched styles, build the render tree, run layout, paint. Anything that delays an early step (a slow stylesheet, a blocking script) delays everything after it, so the user stares at a blank screen longer.
Why is CSS treated as render-blocking by default?
A stylesheet could change any element's size, position, or visibility, so the browser can't safely paint without it. By default it waits to build the render tree and paint until it has parsed all the CSS it knows about, rather than painting a page it would have to redraw a moment later.
Why can a `<script>` tag without `defer` or `async` block HTML parsing?
A synchronous script might use document.write or rely on or change the DOM as it is at that point. So the browser pauses parsing, fetches and runs the script, and only then continues building the DOM.
What is 'layout thrashing,' and how does it happen?
It's when code forces the browser to recalculate layout synchronously, again and again, in a loop. Usually it's reading a layout property (like offsetHeight) right after a style change. That forces the browser to flush pending layout immediately instead of batching it once per frame.
A loop reads `element.offsetWidth` and then sets `element.style.width` for each of 50 elements — why is this slow, and how would you fix it?
Reading offsetWidth forces layout to be current, and the style write right after invalidates it again. Alternating across 50 elements can force about 50 separate layout recalculations. Fix it by reading all the values first, then doing all the writes, so the browser can batch them into one layout pass.
Does a reflow ever affect elements other than the one that changed?
Yes. Layout works across the tree. Changing one element's size can move its siblings, resize a parent that sizes to its content, or ripple further depending on the layout mode (normal flow, flexbox, grid).
After layout recalculates, does the browser have to repaint the entire page?
No. Repainting is usually limited to the regions or layers the change affected. Browsers track which areas are 'dirty' and repaint only those. That's partly why moving often-changing content to its own composited layer can help performance.
Is every DOM node represented in the render tree?
No. Nodes that never produce visible output (like <head>, <script>, <meta>) and elements set to display: none are left out. The render tree only holds what needs a box on screen.
Why does the browser start rendering before the entire HTML file has finished downloading?
The HTML parser builds the DOM as bytes stream in. Once enough content and CSS have arrived, the browser can build the render tree and paint a first view, instead of leaving the user on a blank screen until everything downloads.
Why does splitting rendering into separate stages (render tree, layout, paint) make the browser more efficient at handling later changes?
Each stage only reruns when something relevant to it changes. A color change can skip layout and go straight to paint. A transform on a composited layer can skip both layout and paint. With a single step, every change, however small, would rerun the full pipeline.
A page animates an element's `margin-left` in one place and `transform: translateX()` in another, both moving it the same distance — which is likely to feel smoother, and why?
The transform: translateX() version. It can usually run entirely on the compositor, skipping layout and paint. Animating margin-left changes the element's position in the layout, forcing layout (and usually a repaint) on every frame.
What's the more precise, spec-adjacent name for what's often casually called 'reflow,' and what's the equivalent for 'repaint'?
'Reflow' is the layout stage: recalculating size and position. 'Repaint' is the paint stage: filling in pixels from the computed layout. They're the same stages; 'reflow' and 'repaint' just mean they're running again after a change.