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
- 1 and 2: HTML to DOM
- 3: CSS to CSSOM
- Scripts block parsing
- 4: the render tree
- 5: layout (also called reflow)
- 6: paint
- 7: composite
- What triggers which steps when something changes
- Layout thrashing
- DOMContentLoaded vs load
- Core Web Vitals
- How to make pages load faster
- Measuring
- Common mistakes
- How to say it in an interview
- Key words
The big picture
Lesson 3 gave a short version. Here is the full one. The steps are called the critical rendering path:
- Download HTML
- Parse HTML into the DOM
- Download and parse CSS into the CSSOM
- Combine them into the render tree
- Layout: work out the size and position of every box
- Paint: fill in pixels (text, colours, borders, images)
- 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.
| Script | Downloads | Runs | Blocks HTML parsing? |
|---|---|---|---|
<script> | When reached | Immediately after download | Yes |
<script async> | In parallel | As soon as downloaded, any order | Only while running |
<script defer> | In parallel | After HTML is parsed, in order | No |
<script type="module"> | In parallel | Like defer by default | No |
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 change | Steps the browser redoes | Cost |
|---|---|---|
width, height, margin, top, font size, adding or removing elements | Layout, paint, composite | Most expensive |
color, background, box-shadow, visibility | Paint, composite | Medium |
transform, opacity | Composite only | Cheapest |
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
| Event | Fires when |
|---|---|
DOMContentLoaded | The HTML is fully parsed and the DOM is built. Deferred scripts have run. Images may still be loading. |
load | Everything 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.
| Metric | Measures | Good |
|---|---|---|
| LCP (Largest Contentful Paint) | Loading: when the biggest image or text block appears | 2.5 seconds or less |
| INP (Interaction to Next Paint) | Responsiveness: delay between a user action and the screen updating | 200 ms or less |
| CLS (Cumulative Layout Shift) | Visual stability: how much content jumps around | 0.1 or less |
INP replaced an older metric, FID (First Input Delay), in March 2024.
How to make pages load faster
| Problem | Fix |
|---|---|
| Slow LCP | Compress and resize images; use modern formats (WebP, AVIF); preload the main image; serve from a CDN; render on the server (Part 13) |
| Poor INP | Avoid long JavaScript tasks; split work; move heavy work to Web Workers; ship less JavaScript |
| High CLS | Set width and height on images and videos; reserve space for adverts and embeds; use font-display: swap carefully |
| Render-blocking resources | Small CSS; defer scripts; inline only the critical CSS |
| Too many large files | Code 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>withoutdefer. - 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
| Word | Meaning |
|---|---|
| Critical rendering path | The steps from HTML to pixels on screen |
| DOM | The tree of page elements in memory |
| CSSOM | The browser's structure of all CSS rules |
| Render tree | DOM plus CSSOM, only visible elements |
| Layout / reflow | Calculating sizes and positions |
| Paint / repaint | Filling in pixels |
| Composite | Combining painted layers |
| Layout thrashing | Forcing many layouts by mixing reads and writes |
| Core Web Vitals | Google's LCP, INP and CLS metrics |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.