Skip to the content

Lesson 86 of 296

How a page loads: parse, render, paint

What you will learn

  • Every step from receiving HTML to pixels on the screen
  • The DOM and the CSSOM
  • What blocks rendering and why
  • Layout (reflow), paint and composite
  • DOMContentLoaded vs load
  • Core Web Vitals and how to make pages faster
On this page

The big picture

Lesson 3 gave a short version. Here is the full one. The steps are called the critical rendering path:

  1. Download HTML
  2. Parse HTML into the DOM
  3. Download and parse CSS into the CSSOM
  4. Combine them into the render tree
  5. Layout: work out the size and position of every box
  6. Paint: fill in pixels (text, colours, borders, images)
  7. Composite: put the painted layers together in the right order on screen

JavaScript can run at several points and change the DOM or CSSOM, which can send the browser back to earlier steps.

1 and 2: HTML to DOM

The browser reads the HTML as it downloads, from top to bottom, and builds the DOM (Document Object Model): a tree of objects, one for each element, piece of text and comment. JavaScript uses the DOM to read and change the page (Lesson 87).

3: CSS to CSSOM

Each CSS file is downloaded and parsed into the CSSOM (CSS Object Model): all the style rules in a structure the browser can use.

CSS blocks rendering. The browser will not show the page until it has the CSS it needs. Otherwise the user would see unstyled content first, then a sudden jump (called a "flash of unstyled content"). This is why large CSS files make the first view slower.

Scripts block parsing

When the parser meets a normal <script>, it stops building the DOM, downloads the script, runs it, then continues. The script might change the page (for example with document.write), so the browser must wait.

ScriptDownloadsRunsBlocks HTML parsing?
<script>When reachedImmediately after downloadYes
<script async>In parallelAs soon as downloaded, any orderOnly while running
<script defer>In parallelAfter HTML is parsed, in orderNo
<script type="module">In parallelLike defer by defaultNo

Use defer (or modules) for your own scripts (Lesson 14).

4: the render tree

The render tree combines the DOM and CSSOM, and contains only what will be shown. Elements with display: none are not in it. Elements with visibility: hidden are in it (they take up space).

5: layout (also called reflow)

The browser calculates the exact position and size of every box, using the box model (Lesson 17), Flexbox, Grid and the screen size.

6: paint

The browser fills in the pixels: text, colours, backgrounds, shadows, images.

7: composite

Some elements are painted on separate layers (for example those using transform or opacity animations, or videos). The compositor, often using the graphics card (GPU), puts the layers together. Changing only transform or opacity can skip layout and paint, which is why those animations are smooth (Lesson 24).

What triggers which steps when something changes

You changeSteps the browser redoesCost
width, height, margin, top, font size, adding or removing elementsLayout, paint, compositeMost expensive
color, background, box-shadow, visibilityPaint, compositeMedium
transform, opacityComposite onlyCheapest

Reflow = layout is recalculated. Repaint = pixels are painted again. Reflow always causes a repaint; a repaint does not always need a reflow.

Layout thrashing

Reading a layout value (offsetHeight, getBoundingClientRect(), scrollTop) forces the browser to calculate layout immediately if anything has changed. Alternating reads and writes in a loop forces many layouts:

for (const box of boxes) {
  const width = box.offsetWidth;          read: forces layout
  box.style.width = width + 10 + "px";    write: makes layout dirty again
}

Fix: read everything first, then write everything:

const widths = boxes.map((box) => box.offsetWidth);
boxes.forEach((box, i) => {
  box.style.width = widths[i] + 10 + "px";
});

DOMContentLoaded vs load

EventFires when
DOMContentLoadedThe HTML is fully parsed and the DOM is built. Deferred scripts have run. Images may still be loading.
loadEverything has loaded: images, stylesheets, iframes, fonts
document.addEventListener("DOMContentLoaded", () => {
  console.log("DOM ready");
});
window.addEventListener("load", () => {
  console.log("Everything loaded");
});

With defer scripts, the DOM is already ready when your code runs, so you usually do not need DOMContentLoaded.

Core Web Vitals

Google measures real user experience with three Core Web Vitals. They affect search ranking (Lesson 14), and interviewers ask about them.

MetricMeasuresGood
LCP (Largest Contentful Paint)Loading: when the biggest image or text block appears2.5 seconds or less
INP (Interaction to Next Paint)Responsiveness: delay between a user action and the screen updating200 ms or less
CLS (Cumulative Layout Shift)Visual stability: how much content jumps around0.1 or less

INP replaced an older metric, FID (First Input Delay), in March 2024.

How to make pages load faster

ProblemFix
Slow LCPCompress and resize images; use modern formats (WebP, AVIF); preload the main image; serve from a CDN; render on the server (Part 13)
Poor INPAvoid long JavaScript tasks; split work; move heavy work to Web Workers; ship less JavaScript
High CLSSet width and height on images and videos; reserve space for adverts and embeds; use font-display: swap carefully
Render-blocking resourcesSmall CSS; defer scripts; inline only the critical CSS
Too many large filesCode splitting and lazy loading (Lesson 73); compression (gzip or Brotli); caching (Part 15)

Resource hints in the <head>:

<link rel="preconnect" href="https://fonts.example.com">
<link rel="preload" href="/images/hero.webp" as="image">

preconnect opens the connection to another server early. preload downloads an important file before the browser would normally discover it.

Measuring

  • Lighthouse (in Chrome dev tools): a lab test with scores and suggestions.
  • Performance panel: a timeline of everything the browser did.
  • PageSpeed Insights: Lighthouse plus real user data from Chrome users.

Common mistakes

  • Large render-blocking CSS and scripts in the <head> without defer.
  • Images without width and height, causing layout shift.
  • Animating layout properties instead of transform.
  • Reading and writing layout values in a loop.

How to say it in an interview

"The browser parses HTML into the DOM and CSS into the CSSOM, combines them into a render tree of visible elements, calculates layout, paints pixels and composites layers. CSS is render-blocking and classic scripts are parser-blocking, so I use defer or modules. Changing geometry triggers layout and paint, colours trigger paint, and transform and opacity only need compositing. I watch the Core Web Vitals, LCP, INP and CLS, and improve them with optimised images, less JavaScript, code splitting and reserved space for media."

Key words

WordMeaning
Critical rendering pathThe steps from HTML to pixels on screen
DOMThe tree of page elements in memory
CSSOMThe browser's structure of all CSS rules
Render treeDOM plus CSSOM, only visible elements
Layout / reflowCalculating sizes and positions
Paint / repaintFilling in pixels
CompositeCombining painted layers
Layout thrashingForcing many layouts by mixing reads and writes
Core Web VitalsGoogle's LCP, INP and CLS metrics

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.