Ankit KanojiyaBeginner

Evergreen guide by Ankit Kanojiya · Beginner · Frontend

How the Browser Renders a Page: The Critical Rendering Path

A step-by-step look at how a browser turns HTML, CSS, and JavaScript into pixels on screen. Understanding this pipeline is the foundation for diagnosing slow pages.

When a browser receives a response from a server, it does not simply paint the markup to the screen. It runs a sequence of steps known as the critical rendering path. Knowing each step helps explain why some resources block rendering, why layout can be expensive, and where to look when a page feels slow.

The main steps

The browser transforms bytes into pixels through a predictable pipeline:

  • Parse HTML into the DOM (Document Object Model) tree.
  • Parse CSS into the CSSOM (CSS Object Model) tree.
  • Combine the DOM and CSSOM into the render tree, which contains only visible nodes.
  • Run layout (also called reflow) to compute the size and position of each node.
  • Paint pixels into layers.
  • Composite the layers together to produce the final frame.

Each step depends on the output of the step before it, so a delay early in the path pushes everything downstream.

Parsing and the DOM

The HTML parser reads the document top to bottom and builds the DOM incrementally. This is why the order of tags matters. When the parser encounters a resource it must handle before continuing, it can pause.

A plain script tag is parser-blocking. The parser stops, fetches the script, executes it, and only then resumes. That is why script placement and loading attributes matter:

<script src="/app.js" defer></script>
<script src="/analytics.js" async></script>
  • defer downloads the script in parallel and runs it after parsing finishes, in document order.
  • async downloads in parallel and runs as soon as it arrives, in no guaranteed order.
  • Neither attribute blocks the parser while downloading, which keeps the DOM building.

CSS is render-blocking

The browser will not paint meaningful content until it has processed the CSS it needs, because painting with incomplete styles would cause a flash of unstyled content. CSS is therefore render-blocking by default.

Two practical consequences follow:

  • Keep the CSS needed for the first screen small and deliver it quickly.
  • Defer or split stylesheets that are only needed for content further down the page.

Media queries let the browser treat some stylesheets as non-blocking for the initial render:

<link rel="stylesheet" href="print.css" media="print" />

The print stylesheet is still downloaded, but it does not block the first paint for screen users.

Layout and paint

Once the render tree exists, layout computes geometry: how wide each box is, where it sits, how lines wrap. Layout is relative, so changing one element can force recalculation of many others. Reading a layout property such as offsetHeight right after writing to the DOM can trigger a synchronous reflow, sometimes called layout thrashing:

// Forces the browser to flush layout on every iteration
for (const el of items) {
  el.style.width = el.offsetWidth + 10 + 'px';
}

Batch reads and writes instead. Read all measurements first, then apply all writes, so the browser performs a single layout pass.

Paint fills in pixels: text, colors, shadows, borders. Compositing assembles painted layers, which is where the GPU often helps.

Properties that skip layout

Not every visual change requires a full trip through layout and paint. Animating transform and opacity can be handled by the compositor alone, which is far cheaper than animating properties like width, top, or margin:

/* Cheap: compositor can handle this */
.card { transition: transform 200ms ease; }
.card:hover { transform: translateY(-4px); }

Prefer transform-based animation for smooth motion, and avoid animating layout-triggering properties in a loop.

Measuring the path

Browser developer tools expose this pipeline directly. The Performance panel records each phase so slow layout or long scripting can be seen on a timeline. Key metrics tied to the rendering path include:

  • First Contentful Paint, the moment the first text or image appears.
  • Largest Contentful Paint, the moment the main content is likely visible.

Practical takeaways

  • Put render-blocking CSS in the document head and keep it lean.
  • Use defer for scripts that manipulate the DOM, and async for independent scripts.
  • Inline a small amount of critical CSS for the first screen when startup speed matters, and load the rest afterward.
  • Avoid interleaved DOM reads and writes to prevent forced reflows.
  • Animate transform and opacity rather than layout properties.

The critical rendering path is not a single bottleneck but a chain. Small improvements at the blocking steps, CSS delivery and script execution, usually produce the largest gains in perceived speed.