Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

How Browsers Render a Page

Understand how the browser turns HTML, CSS and JavaScript into pixels, which resources block parsing or rendering, and how to shorten the path to the first paint.

Once the bytes of a page arrive, the browser still has a lot of work to do before anything appears. The sequence of steps that turns HTML, CSS and JavaScript into pixels is often called the critical rendering path. Anything that sits on that path delays the first paint, so optimising it means removing, shrinking or deferring everything the first view does not need.

You saw the network half in how a page loads. This lesson covers what happens inside the browser.

The steps

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

The DOM and CSSOM combine into the render tree, which flows through layout, paint and compositing.

  1. Parse HTML into the DOM. Tokens become elements, and the tree grows as bytes stream in.
  2. Parse CSS into the CSSOM. Unlike HTML, CSS must be fully known before use, because a later rule can override an earlier one.
  3. Build the render tree. Only visible elements are included; display: none elements are left out.
  4. Layout. The browser computes the size and position of every box.
  5. Paint. Boxes become pixels: text, colours, borders, images, shadows.
  6. Composite. Layers are combined, usually on the GPU, in the right order.

Render-blocking and parser-blocking

The browser will not paint before it has the CSSOM, because showing unstyled content and then restyling would flash. So CSS is render-blocking. Classic scripts are parser-blocking: when the parser meets a <script>, it stops, fetches and runs it, because the script might change the document.

A script after a stylesheet also waits for that stylesheet, since scripts can read computed styles. A slow stylesheet can therefore delay scripts, which delay parsing.

Resource Blocks parsing? Blocks rendering?
CSS <link> No Yes
Classic <script src> Yes Yes (parsing stops)
<script defer> or type="module" No No
<script async> Only while it runs Only while it runs
Images No No

Big <script> tags in the <head> without defer are among the most common causes of a slow first paint. Add defer unless the script truly has to run during parsing.

Feel the blocking

This script simulates 300 ms of work while the page is being parsed. The second script, placed later, can only run after the first one finishes.

A parser-blocking script

The first script sees one heading, because the parser had not reached the second yet. If it needed the whole page, defer would be the right fix.

Shorten the critical path

Describe any page by three numbers: the critical resources (files blocking the first render), the critical bytes and the path length (round trips in sequence). The usual fixes shrink one of them:

  1. Inline the critical CSS needed for the first screen and load the rest later.
  2. Defer JavaScript with defer or type="module", and split code so only the first view's code loads first.
  3. Use media to unblock CSS that does not apply to the current device.
  4. Minify and compress (compression).
  5. Avoid chains. An @import inside CSS is found only after the first file loads.
HTML
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/css/print.css" media="print">
<link rel="stylesheet" href="/css/wide.css" media="(min-width: 1200px)">

On a phone, the last two files do not block the first paint.

Count the blocking stylesheets

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is CSS render-blocking?

    Rendering before the CSSOM is ready would flash unstyled content, so the browser waits.
  2. 2. What does a classic script without defer do to the parser?

    The script might modify the document, so the parser waits for it.
  3. 3. Which stylesheet does NOT block the first render on a phone?

    A media value that does not match the current device makes the file non-blocking.
  4. 4. What is the main problem with @import in CSS?

    Imports create sequential requests instead of parallel ones.

Summary

  • The path is HTML to DOM, CSS to CSSOM, render tree, layout, paint, composite.
  • CSS blocks rendering; classic scripts block parsing.
  • Use defer, inline critical CSS, media attributes and avoid @import chains.
  • Shorten the path by sending fewer, smaller critical resources in fewer round trips.

Next: Lab Data and Field Data