On this lesson's example page, the HTML reaches the phone in 400 ms. But the screen only shows something at 1,350 ms. For almost a second the browser isn't being lazy: it's waiting for something before it's allowed to draw.
The route it takes from receiving HTML to drawing the first pixels on screen is the critical rendering path. It's called critical because everything on this route has to be done before the visitor sees anything. Picture cooking fried rice: the rice, the spices and the wok must be ready before you start, while the crackers can come later. If you wait for the crackers to be fried before you start cooking, the meal is late too. Your job: tell apart what's required and what can come later.
From HTML to pixels
- DOM. The browser reads (parses) the HTML from top to bottom and builds the DOM, a tree of every element on the page.
- CSSOM. All the CSS is built into the CSSOM, the set of style rules.
- Render tree. The DOM and CSSOM are combined. It holds only elements that show, so anything with
display: noneis dropped. - Layout. The browser works out the position and size of every element.
- Paint. The browser colours in the pixels: text, colours, borders, images.
- Composite. If the page is split into several layers, the layers are stacked into one final picture.
The first moment the browser draws anything on screen is called the first paint. The sooner the first paint, the sooner the visitor knows the page is alive.
What holds rendering back
A render-blocking resource makes the browser put off drawing the page until that resource is done.
- CSS from
<link rel="stylesheet">is render-blocking by default. The browser doesn't want to draw the page without styles and then suddenly change its shape. - A plain
<script src="...">, withoutdeferorasync, is even parser-blocking: the browser stops reading the HTML, downloads that script, runs it, and only then reads on. The HTML below it isn't in the DOM yet, so there's nothing to draw. - Images and fonts don't hold back the first paint. But text that uses a custom font stays invisible until the font arrives (the browser hides it for up to about 3 seconds).
The example page
This is the <head> of the page used in the Lab. The CSS for the top of the page is already written straight into <style>, so non-critical.css only holds styles for parts that aren't visible yet:
<head>
<title>Landing Page</title>
<style>/* critical CSS: header and hero */</style>
<link rel="stylesheet" href="non-critical.css">
<script src="app.js"></script>
</head>
On a slow phone network, the timing is roughly this (a simulation):
| Resource | Starts | Ends | Holds back? |
|---|---|---|---|
index.html |
0 ms | 400 ms | |
non-critical.css (60 KB) |
400 ms | 1,000 ms | rendering |
app.js (120 KB) |
400 ms | 1,100 ms, then runs 150 ms | the parser |
hero.woff2 (the heading font) |
1,300 ms | 1,600 ms |
The parser stops at app.js from 400 ms until the script finishes running at 1,250 ms, then needs 50 ms to read the rest of the HTML. The first paint only comes at 1,350 ms. The heading font is only found to be needed once the render tree exists, so the heading only shows at 1,600 ms.
One at a time, then all together
Before you look at the table, guess: if you only add defer to the script, where does the first paint move from 1,350 ms? To 500 ms, to 1,050 ms, or does it stay at 1,350 ms?
| What changed | First paint | Hero heading shows |
|---|---|---|
| nothing yet | 1,350 ms | 1,600 ms |
defer only |
1,050 ms | 1,300 ms |
media="print" only |
1,350 ms | 1,600 ms |
defer and media="print" |
500 ms | 750 ms |
| plus preloading the font | 500 ms | 700 ms |
With defer alone, the first paint moves to 1,050 ms. The parser isn't held anymore, but non-critical.css still holds rendering until 1,000 ms. The other way round, media="print" alone changes nothing, because the script still holds the parser. Only the two together bring the first paint down to 500 ms.
Three fixes in the head
<head>
<title>Landing Page</title>
<style>/* critical CSS: header and hero */</style>
<link rel="preload" href="/fonts/hero.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
<script src="app.js" defer></script>
</head>
rel="preload"means "download this now, it will definitely be used".as="font"says what kind of file it is,type="font/woff2"its format.crossoriginis required for fonts: fonts are always fetched in CORS mode, and a preload withoutcrossorigindoesn't match the real request, so the font is downloaded twice.media="print"says this stylesheet is for printing, so it doesn't hold back what's on screen. The browser still downloads it. Once it's done,onload="this.media='all'"switches its media toall, and the styles apply on screen.defermakes the script download while the HTML keeps being read, then run after the HTML has been read in full, in the order it's written.asyncalso downloads alongside, but runs the script as soon as it's downloaded, in no guaranteed order. It suits scripts that stand on their own, such as analytics.<script type="module">(what Vite writes in its build output) already behaves likedeferwithout writing it: in Chromium 156, a module script in<head>ran only after the HTML had been read in full. Addasyncif that module script may run as soon as it's downloaded.
The same works for the page's main image: <link rel="preload" href="hero.webp" as="image"> (images don't need crossorigin). So when a page has a big CSS file and several scripts in <head>, the three most effective steps are: put the critical CSS straight into <style>, give the scripts defer, and preload the biggest image.
⚠️ When do the head tricks backfire?
- Preloading a font without
crossorigin. There's no error, but the font is downloaded twice and the preload is wasted. media="print"withoutonload. The CSS only applies when the page is printed, never on screen. The two attributes must be on the same<link rel="stylesheet">tag.deferon a script with nosrc.deferonly works on scripts that download a file. On an inline script,deferis ignored.- The
media="print"trick on critical CSS. The page shows up unstyled for a moment and then jumps (FOUC, a flash of unstyled content). The trick is only for CSS the first screen doesn't need. asyncon a script that needs order. Ifapp.jsneeds a library from an earlier script,asyncmay run it first. Usedefer.onloadon a site with a strict CSP. A CSP (Content Security Policy, a security header that limits scripts) without'unsafe-inline'blocks anonloadwritten in a tag.non-critical.cssstaysmedia="print", so its styles never apply on screen. On such a site, a script the CSP allows must change the media, for example a separate.jsfile.
TL;DR: The browser turns HTML into pixels through the DOM, CSSOM, render tree, layout, paint and composite. CSS holds rendering by default, and a <script> without defer or async holds the parser, so both delay the first paint. Fix the <head> in three steps: defer on the script that has a src, media="print" plus onload="this.media='all'" on CSS that isn't critical, and <link rel="preload" as="font" crossorigin> for an important font. In this example the first paint drops from 1,350 ms to 500 ms, and the hero heading from 1,600 ms to 700 ms.
⚡ From HTML to pixels
Guess first: which step works out the position and size of every element?
Layout. After it, paint colours in the pixels, and composite stacks the layers into one picture.
Meaning: The browser builds the DOM from the HTML and the CSSOM from the CSS, combines them into the render tree, then runs layout, paint and composite. The first moment anything is drawn on screen is called the first paint.
⚡ What holds rendering back
Write:
<link rel="stylesheet" href="non-critical.css">
<script src="app.js"></script>
Meaning: CSS holds rendering back by default. A <script> without defer or async even holds the parser: the browser stops reading the HTML until that script is downloaded and has run. On the example page, the first paint only comes at 1,350 ms.
⚡ Three fixes in the head
Write:
<link rel="preload" href="/fonts/hero.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
<script src="app.js" defer></script>
Guess first: if only the script gets defer, where does the first paint move?
To 1,050 ms, not 500 ms. The parser isn't held anymore, but non-critical.css still holds rendering until 1,000 ms. Only the two together bring the first paint down to 500 ms.
Meaning: defer runs the script after the HTML has been read in full. media="print" keeps the CSS from holding the screen, then onload switches it to all. A font preload needs crossorigin, or the font is downloaded twice.