How Browsers Render a Page

Beginner · 25 min

The critical rendering path — how raw HTML and CSS become pixels, and what makes some page updates expensive.

Before thisHTTP Messages, Status Codes & Headers

JSConf · 25 min

How Browsers Render a Page

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by JSConfReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A browser doesn't display HTML and CSS directly — it parses them into two in-memory tree structures, the DOM and the CSSOM, before anything can be drawn.
  • The render tree is the DOM and CSSOM combined and filtered down to only the nodes that will actually be visible, which is why a display:none element never reaches layout at all.
  • Layout calculates the exact size and position of every visible node in pixels; paint fills in the actual pixels — colors, text, images — for each one.
  • Compositing assembles painted layers in the right order and draws the final frame, and some changes (like transforms) can skip straight to this stage without redoing layout or paint.
  • Changing something that affects geometry (like width or font-size) forces a reflow that can cascade to other elements, which is why animating geometric properties is far more expensive than animating transform or opacity.

From text file to picture

You’ve now seen how a request leaves your browser and a response comes back. That response, for a normal page, is just text — an HTML file, maybe a linked CSS file, maybe some JavaScript. None of that text is a picture. Somewhere between “the browser has some text” and “you can see a page,” a huge amount of work happens, and that work is called the critical rendering path. It’s called “critical” because it’s the minimum sequence of steps that has to complete before anything appears on screen — understanding it explains both why pages are structured the way they are and why certain changes to a page are cheap while others quietly tank your frame rate.

Parsing HTML into the DOM

The browser reads the HTML byte by byte and builds a tree of objects out of it called the Document Object Model, or DOM. Every tag becomes a node; nesting in the markup becomes parent-child relationships in the tree. This isn’t just a formality — the DOM is the actual, live, in-memory representation of your page that JavaScript manipulates when you call something like document.querySelector. The text file is gone at this point; what exists in memory is a structured tree the browser can walk, query, and modify.

While parsing HTML, if the browser hits a <link> to a stylesheet or a <script> tag, it has to go fetch that resource too — which means everything from the previous two lessons (DNS, TCP, the request/response cycle) can happen again, mid-parse, for every linked file a page needs.

Parsing CSS into the CSSOM

Stylesheets go through a parallel process, building the CSS Object Model, or CSSOM — a tree representing every style rule and which elements they apply to, with cascade and specificity already resolved into a single computed value per property per element. This is why CSS is said to “cascade”: a paragraph’s final color isn’t one line in one file, it’s the browser resolving every rule that could apply to that paragraph — a tag selector, a class, an inline style — down to one winning value, for every single property, on every single element.

Here’s a detail worth knowing: CSS is considered “render-blocking.” The browser won’t paint anything until it has the full CSSOM, because showing unstyled content and then abruptly restyling it would be a worse experience than a brief blank screen. This is one reason stylesheets belong in the <head> and why a slow-loading CSS file can stall an entire page, even one with tiny HTML.

Building the render tree

With both the DOM and the CSSOM built, the browser combines them into the render tree: a tree of only the nodes that will actually produce visible output, each one annotated with its final computed styles. An element styled display: none is in the DOM — JavaScript can still find it, still read its attributes — but it’s excluded from the render tree entirely, because it will never occupy space or produce pixels. This is different from visibility: hidden, which does occupy space and remains in the render tree; it just paints nothing visible. That distinction — present in the render tree versus not — is exactly why one hides an element without affecting layout around it, and the other removes it from the flow completely.

Layout: from tree to coordinates

The render tree knows what should appear and with what styles, but not yet where, or at what size, in actual pixels. Layout (sometimes called reflow) walks the render tree and computes the exact box — width, height, x and y position — for every single node, starting from the viewport’s dimensions and working down.

This step is inherently cascading: a parent’s width can constrain a child’s available space, and a child growing taller can push every sibling after it further down the page. That’s precisely why layout is one of the more expensive operations in this entire pipeline — a single change to one element’s size can force the browser to recompute geometry for a large portion of the tree, not just the element you touched.

Paint and compositing

Once every box has a position and size, paint fills in the actual visual appearance within each box — background colors, borders, text, images, shadows — essentially turning each element into a set of drawn pixels. Complex pages are often broken into multiple layers (think of a fixed header sitting above scrolling content as a concrete example of separate layers), each painted independently.

The final stage, compositing, takes those painted layers and assembles them in the correct order into the single frame you actually see, handling things like overlap, opacity, and transforms. This is the stage where a well-optimized animation can live entirely — changing an element’s transform or opacity can often be handled purely by recombining existing painted layers differently, with no new layout or paint work at all.

Why this determines what’s expensive

This whole pipeline is the real answer to “why is my animation janky.” Changing a property that affects geometry — width, top, font-size, adding a margin — forces layout to rerun, which can ripple to other elements, followed by a repaint, followed by recompositing: the full pipeline, every frame. Changing transform or opacity, by contrast, can frequently skip layout and paint entirely and go straight to compositing, which is dramatically cheaper and is why performance-conscious interfaces lean on those two properties for anything animated.

The pipeline also explains why display: none versus visibility: hidden behave so differently, why linking a huge stylesheet in the <head> delays the first paint of an otherwise-tiny page, and why JavaScript that reads a layout property (like offsetHeight) right after writing a style change can force a synchronous, unscheduled layout recalculation — a performance trap often called “layout thrashing.” None of this is incidental trivia; it’s the direct mechanical consequence of the five-stage pipeline you’ve just walked through: parse, build the CSSOM and DOM, construct the render tree, lay out, paint, and composite.

Check yourself

  1. 01Why can't a browser just draw HTML tags directly onto the screen?

    Because HTML and CSS are text — a browser first has to parse them into structured, in-memory tree representations (the DOM and CSSOM) that it can actually compute positions and styles from.

  2. 02What's the difference between the DOM and the render tree?

    The DOM represents every element in the HTML regardless of whether it's visible. The render tree combines the DOM with computed styles and excludes anything that won't actually be drawn, like a display:none element.

  3. 03What's the difference between layout and paint?

    Layout calculates the exact size and position of each visible element, in pixels. Paint takes those calculated boxes and fills in the actual visual detail — color, text, borders, images.

  4. 04Why do performance guides recommend animating transform and opacity instead of width or top?

    Because transform and opacity changes can often be handled purely at the compositing stage, while changing width or top forces the browser to redo layout and repaint, which is far more expensive work per frame.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark