Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท CSS and Rendering

The Critical Rendering Path

Follow the steps a browser takes from HTML bytes to pixels: DOM, CSSOM, render tree, layout, paint and composite, and learn which steps you can speed up.

Between receiving HTML and showing pixels, the browser runs a pipeline called the critical rendering path. Every step is a place where your code can help or hurt. If you know the steps, performance advice such as "avoid layout thrashing" or "animate transform, not left" stops being a list of rules and becomes obvious.

The pipeline

flowchart LR
  H["HTML"] --> D["DOM"]
  C["CSS"] --> S["CSSOM"]
  D --> R["Render tree"]
  S --> R
  R --> L["Layout"]
  L --> P["Paint"]
  P --> K["Composite"]

DOM and CSSOM are built separately, then combined.

  1. Parse HTML into the DOM, the tree of elements. The parser works incrementally, so the browser can start before the whole file has arrived.
  2. Parse CSS into the CSSOM, a tree of style rules. Styles cascade, so the browser cannot use a half-built CSSOM safely.
  3. Build the render tree: only visible elements, each with its computed styles. display: none elements are left out, visibility: hidden ones are kept.
  4. Layout (also called reflow) computes the size and position of every box.
  5. Paint turns boxes into pixels on layers: backgrounds, borders, text, shadows.
  6. Composite combines the layers, usually on the GPU, in the right order to make the final frame.

What blocks what

  • CSS is render-blocking. The browser will not paint until it has the CSS that applies, because painting unstyled content and then restyling it would flash.
  • Classic scripts are parser-blocking and also wait for pending CSS, since a script might read computed styles.
  • Images and fonts are not render-blocking, though they can change layout when they arrive (see layout shifts).

That gives the main strategy of the whole course:

Send the HTML early, keep the render-blocking resources few and small, and let everything else load without stopping the first paint.

Watching the pipeline in the browser

The Performance API records key moments, such as first-paint and first-contentful-paint. This read-only snippet works in the DevTools console of a normal page:

JavaScript
for (const entry of performance.getEntriesByType("paint")) {
  console.log(entry.name, Math.round(entry.startTime), "ms");
}

The first contentful paint (FCP) is the first time any text or image is drawn, so it is the first sign that CSS, HTML and fonts cooperated.

Layout and paint are not free

Layout depends on the whole tree. Changing the width of a high-up element may require recalculating hundreds of descendants. Paint costs depend on the area and complexity: large blurs, shadows and gradients are expensive.

Each style change triggers a different part of the pipeline:

You change Work needed
width, height, margin, top, font-size style โ†’ layout โ†’ paint โ†’ composite
color, background, box-shadow style โ†’ paint โ†’ composite
transform, opacity style โ†’ composite (on its own layer)

That table is why animations of transform and opacity are smooth, and animations of width or top are not.

Try it: measure a layout

This demo forces the browser to lay out a big list and measures the time. The numbers depend on your device, so the code only checks relative results.

Layout cost of many nodes

Notice how the cost grows with the number of nodes. Later lessons show how content-visibility and virtualization avoid rendering what nobody sees.

Reading the order of events

Open a page, then watch this page-load log. It records when the browser reaches each milestone and illustrates that painting can happen before the load event:

Milestones of a page load

The sandboxed preview may hide paint entries, but the order of the first three milestones is reliable.

Ways to shorten the path

  • Reduce the amount of critical resources: inline the small critical CSS, defer the rest (next lesson).
  • Reduce their size: minify and compress.
  • Reduce round trips: avoid chains where CSS imports CSS, or where a script discovers another script late. Preload what is needed immediately.
  • Keep the DOM small: fewer nodes mean faster style, layout and paint (aim for fewer than about 1,500 nodes on a typical page).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two structures are combined into the render tree?

    The render tree combines the content (DOM) with the computed styles (CSSOM) for visible elements.
  2. 2. Why is CSS called render-blocking?

    Painting before styles are known would show unstyled content that flashes.
  3. 3. Which property change can skip layout and paint and go straight to compositing?

    Transform and opacity changes can be handled by the compositor on their own layers.
  4. 4. Is an element with display none in the render tree?

    display none removes the element from the render tree. visibility hidden keeps it, but invisible.

Summary

  • The pipeline is DOM + CSSOM, render tree, layout, paint, composite.
  • CSS blocks rendering; scripts block parsing unless deferred.
  • Changes to layout properties are the most expensive; transform and opacity are the cheapest.
  • Shorten the path by sending fewer, smaller, earlier critical resources.

Next: Render-Blocking CSS and Critical CSS