Critical Rendering Path Explained: DOM, CSSOM, Paint

How the browser turns HTML and CSS into pixels: DOM, CSSOM, render tree, layout, paint and composite, and what moves the first paint earlier.

The critical rendering path: common questions

Why is CSS render-blocking?

The browser will not paint until it has built the CSSOM, so it does not show unstyled content. Every stylesheet in the head delays the first paint until it downloads and parses.

Related chapters

Keep learning

Overview

HTML bytes Built as bytes stream in Bytes become tokens, tokens become nodes. The parser can stop here: a classic <script> pauses it. CSS bytes Not incremental. A rule at the end of the file can override one at the top, so the browser waits for the whole stylesheet. Render tree DOM × CSSOM Needs both Only nodes that produce boxes. display:none drops out. visibility:hidden stays, it still takes space. Render tree + viewport Main thread Every box gets x, y, width and height. Change the viewport width and this whole step runs again. Draw commands: text, colors, borders, shadows, images. Recorded per layer, then rasterized into pixels. Compositor thread / GPU Layers are stacked into the frame. transform and opacity can change here without layout or paint. Chapter 68 · critical rendering path Nothing paints until the browser has the DOM and the CSSOM The critical path is the shortest chain of downloads and work between the first byte and the first pixel. Make the chain shorter and the page appears sooner. Part A · the first paint

4. What you decide for the first paint

Three numbers Three numbers: 3 critical resources, 48 kilobytes, 3 round trips. 3 resources 3 round trips Critical resources (files that block first paint), critical bytes, and critical path length (round trips in a chain). Each optimisation below lowers one of the three. The first 14 KB HTML 6 KB plus critical CSS 7 KB fits under the 14 KB first round trip. 14 KB · trip 1 TCP slow start sends about ten packets (≈14 KB) in the first round trip. HTML plus inlined critical CSS that fits there can paint after one trip. One byte over costs a second. CSS blocks render, not parse Parsing runs the whole time. Paint waits until styles.css arrives. DOM grows only now The parser keeps building the DOM. The pixels wait. media="print" or a query that does not match downloads at low priority and blocks nothing. Scripts block the parser Classic script: parsing stops while it downloads and runs. Defer: parsing continues, the script runs at the end. keeps parsing A classic script stops the parser and also waits for every stylesheet above it, because it may read styles. Use defer for app code, async for independent code, and remember type="module" is deferred by default. The preload scanner What the preload scanner can and cannot see <img src> <link href> <script src> URLs built in JS, CSS url(), @import, fonts While the parser is stuck, a second scanner reads ahead for URLs in the markup. It cannot see URLs built in JS, fonts in CSS, CSS background images, or @import. Those want <link rel="preload">. Fonts are found last HTML, then CSS, then the render tree, and only then the font request. render tree The server is on the path 103 hints TTFB is the first link. Flush the <head> early while the body streams. 103 Early Hints lets the browser start CSS and font fetches before the HTML status is even known. Measure the chain LCP split into TTFB, load delay, load duration, and render delay. load delay Performance panel: the network track over the main thread shows who waited for whom. In the field, split LCP into TTFB, load delay, load duration, and render delay. The biggest slice is the next fix. Part B · every frame after it

8. What you decide for every frame

requestAnimationFrame, not timers Timer writes drift against the frames: some frames get two writes, some get none. requestAnimationFrame writes once at the start of every frame. A timer of 16 ms is not a frame. It drifts, so one frame gets two style writes (one wasted) and the next gets none (a stutter). requestAnimationFrame runs your write at the start of the next frame, once, and pauses in background tabs. One write per frame for scroll Five scroll events store the latest scrollY. One animation frame does one DOM write. scroll ×5 1 × rAF 1 DOM write Scroll and pointer events can fire several times per frame. Store the value, schedule one frame if none is pending, and write the DOM there. Passive listeners keep scrolling off the main thread. Heavy math goes to a Worker Main thread posts data to a worker, the worker sorts, main thread applies the result to the DOM. main: postMessage worker: sort 50k main: write DOM Read, then write Interleaved reads and writes force a layout each time. Batched: all reads, then all writes, one layout. Any read of geometry (offsetWidth, getBoundingClientRect, scrollTop, getComputedStyle) after a write forces a synchronous layout. Batch reads first, writes second. FastDOM schedules both phases for you across components. Simple selectors Selector cost body.on .list .box:nth-child(2n) on 5,000 nodes Style recalculation matches selectors right to left for every affected element. Toggling a class on body re-matches the whole tree. Put the class on the elements that change, and keep selectors flat. Layers cost memory One promoted card adds one layer of about a quarter megabyte. Promoting everything adds 800 layers, about 190 megabytes. GPU memory Paint area and blur What makes paint expensive two changed dots in opposite corners → repaint the whole layer large box-shadow blur, filter: blur() A layer repaints the union of everything that changed in it. Two small changes far apart repaint almost the whole layer. Blur radius multiplies paint cost. DevTools Rendering → Paint flashing shows the area. FLIP the layout animation First: measure. Last: apply the end state and measure. Invert: transform back. Play: animate the transform to zero. Play: transform → 0 Need to animate a size or position change? Measure the start, apply the end, measure again, set a transform that puts it back, then animate that transform to zero. One layout up front, compositor-only frames after.

1. Six steps from bytes to pixels

Click a step. Two inputs, one merge, three steps of work. Rendering steps

{current.name}

A DOM tree: html contains head and body. body contains h1, p, and img. html head body h1 p div.modal img Three rules. The last rule sets h1 color to navy and overrides the first rule that set it to red. The render tree keeps body, h1, p, and img. head and the hidden modal are dropped. body h1 · navy p div.modal · display:none img Boxes with coordinates: h1 at y 0, p at y 56, img at y 112. h1 · 0,0 · 320×40 p · 0,56 · 320×46 img · 0,112 · 320×70 The same boxes, now filled with text color, background, and image pixels. drawText(“Hello”, navy) drawRect · drawText drawImage(hero.jpg) Three layers stacked: page, a sticky header, and a toast. The GPU combines them into one frame. page sticky header toast · transform connect + ask found late parser waits parse body Main thread Text visible First paint The parser stopped at app.js. app.js then waited for styles.css, because a script may read styles. Two downloads in a row before any body exists. The parser stopped at app.js until it downloaded and ran. The body could not be built during that wait. The DOM was ready early. The pixels waited one more round trip for styles.css. Everything first paint needs arrived in the HTML response. One round trip after the connection, pixels. The font was found only after styles applied, then text stayed invisible until it arrived. Text painted in a fallback font, then swapped when the font arrived. With no preload scanner, hero.jpg was not even requested until the script finished.

2. Build the waterfall, move the first paint

Change one tag in the <head>. Watch which bars move right. <link> file Inline <style> Script in <head> Web font display: block display: swap Preload scanner parse HTML parser waiting run JS layout + paint first paint critical resources round trips in the chain <script src> Parser stops, fetches, runs, resumes. In order. <script async> Fetch in parallel. Runs the moment it lands. Order not kept. <script defer> Fetch in parallel. Runs after parsing, in order, before DOMContentLoaded. <script type="module"> Deferred by default. Its imports are more fetches in the chain.

3. Four ways to load a script

HTML parsing script download script runs Main thread Every frame that had work made its deadline. The math ran in a Worker, so the main thread only applied the result.

5. After the first paint, every frame has a deadline

The screen refreshes 60 times a second (16.7 ms), or 120 times on newer phones (8.3 ms). JavaScript, style, layout, paint and composite all have to fit before the next refresh. Schedule the update body.on .list .box:nth-child(2n) Move the math to a Worker paint / new frame work per frame frames on time janky frames frames with no update

6. Read, write, read, write: layout thrashing

Interleaved: every read after a write forces a layout. Batched: reads first, then writes, one layout. Run both loops Run again Press run. The numbers come from your machine. Geometry changed. Every box after it may move, so layout runs, then paint, then composite. Same geometry, new pixels. Layout is skipped. The browser usually promotes a transform while it animates, but the first frame can still paint. will-change makes the layer ready ahead of time. The card is its own layer. The compositor moves the bitmap without layout or paint, even while JS is busy. One page layer. The browser promotes on its own when it is sure. One extra 320×200 layer: 320 × 200 × 4 bytes ≈ 0.25 MB. Cheap, and moves skip paint. 800 layers, each a bitmap in GPU memory. On a phone this can be slower than no layers at all, and can crash the tab.

7. Skip steps: what you animate, and which layer it lives on

Each change re-enters the path at a different step. Later is cheaper. A layer lets a move skip paint, and costs memory. browser decides mid phone budget

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.