You've moved the header and hero CSS into a <style> in the HTML, just like the advice you read said. You measure again, and the first paint is still 1,250 ms. Not one millisecond earlier. What you did is only half of the technique.
Critical CSS is the least CSS needed to draw the part of the page that's visible as soon as it opens, called above the fold (above the "fold", the bottom edge of the first screen). That CSS is written straight into a <style> in the HTML, and the rest loads later without holding up rendering. Picture moving house. The things you need for the first night, like a toothbrush and a change of clothes, go in a small suitcase you carry yourself. Everything else goes on a truck that only arrives tomorrow. You can go to sleep that night without waiting for the truck. The small suitcase is the critical CSS, the truck is styles.css.
Why CSS holds up rendering
In the Critical Rendering Path lesson you learned that <link rel="stylesheet"> is render-blocking. The browser won't draw the page before all its CSS has arrived, so the page doesn't show unstyled and then suddenly change shape. The trouble is that styles.css usually holds styles for the whole site: the footer, modals, other pages. The first screen only needs a small part of it.
The example page in the Lab, a coffee shop with a header, a hero and a product list (simulated numbers, a slow phone network):
index.htmlis 8 KB and finishes at 300 ms.styles.cssis 120 KB and downloads from 100 ms to 1,200 ms.- The CSS the header and hero need is only 6 KB: 2 KB for the header, 4 KB for the hero.
- Once everything it waits for is ready, the browser takes 50 ms to draw.
Two steps, not one
| Approach | First paint | Header and hero at the first paint |
|---|---|---|
| plain link, no critical CSS | 1,250 ms | styled |
critical CSS in <style>, link still plain |
1,250 ms | styled |
| link deferred, no critical CSS | 350 ms | unstyled, then jumps at 1,250 ms |
critical CSS in <style> and link deferred |
350 ms | styled |
That's why the first paint at the start stayed at 1,250 ms. As long as the <link rel="stylesheet"> is plain, the browser still waits for styles.css, however much CSS has been inlined. The other way round, deferring the link with no critical CSS makes the page show unstyled and then jump when styles.css arrives. That jump is called FOUC (flash of unstyled content), and the shift counts toward CLS. Critical CSS takes both steps together: inline what's critical, then defer the rest.
Code: critical CSS, then the rest deferred
<head>
<style>
/* Critical CSS: only the header and hero, 6 KB */
body { margin: 0; font-family: system-ui, sans-serif; }
.header { display: flex; height: 56px; padding: 0 16px; background: #1e293b; color: #fff; }
.hero { padding: 48px 16px; text-align: center; background: #fef3c7; }
</style>
<!-- The rest: downloaded without holding up rendering -->
<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
</head>
- The
<style>in<head>holds rules for first-screen elements only. The browser can use it right away, with no extra request. media="print"says this stylesheet is for printing, so it doesn't hold up what's on screen. Once it has downloaded,onload="this.media='all'"switches it toalland its styles apply.<noscript>is the backup for visitors who turn JavaScript off. Without JavaScript,onloadnever runs, so without<noscript>the CSS never applies on screen.
A second, equally non-blocking way; use only one of them:
<link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
rel="preload" as="style"downloads the CSS at high priority without using it.onloadturns it intorel='stylesheet'once it's done.this.onload=nullstopsonloadrunning twice, because in some browsers changingrelfiresonloadagain.<link>has noasyncordeferattribute. If you write one, the browser ignores it and the CSS still holds up rendering.
What goes into the critical CSS
- Everything visible on the first screen: the header, the navigation, the hero (heading, image, button), a cookie banner that shows right away, and the
@font-faceof the fonts used there. - Not for: the footer, a modal that only appears after a click, sections below the fold, other pages.
- The fold sits in a different place on a phone and a laptop. Extract the critical CSS for several screen sizes at once.
Don't pick it by hand. The critical tool (npm) opens the page in a browser with no window (headless) at given screen sizes, then takes the CSS used by elements above the fold. Beasties (the successor to Critters) works at build time: it reads the built HTML, inlines the CSS that HTML uses, and loads the rest without holding up rendering.
Pros and cons
- Pro: the first paint is much sooner. In this example, from 1,250 ms to 350 ms.
- Keep it under about 14 KB. A server's first send is usually only about 14 KB. 8 KB of HTML + 6 KB of critical CSS = 14 KB, just right. If the product CSS (30 KB) is inlined too, the HTML grows to 44 KB, needs extra sends, and in the Lab the first paint slips to 650 ms.
- Not cached on its own. Inline CSS travels inside every HTML page, so it's downloaded again on every page change.
styles.cssis downloaded once, then comes from the cache. - The rules are doubled. The header and hero rules sit in
<style>and instyles.css. Let a build tool copy them, so the two never drift apart.
⚠️ Why doesn't the first paint move, or why does the page jump?
- Inlining the critical CSS but keeping a plain link. The first paint doesn't change at all, because a plain link still holds up rendering.
- Missing an element on the first screen. That element shows unstyled and then jumps (FOUC), and the shift adds to CLS.
- Inlining all the CSS. The HTML swells past 14 KB, can't be cached, and the first paint gets slower instead.
- Writing
asyncordeferon<link>. Those attributes don't exist for<link>. Usemedia="print"withonload, orrel="preload" as="style"withonload. - Your site uses a strict CSP. A CSP (Content Security Policy) is a security header that limits scripts. Without
'unsafe-inline', a CSP blocks anonloadwritten in an HTML tag, in both ways above. The link staysmedia="print"or staysrel="preload", so the page stays unstyled for good (tested in Chromium 156 withscript-src 'self'). On such a site, use Beasties'preload: 'media-script'mode: one script at the end of<body>changes the media, and the CSP lets it run through anonceor a hash.
TL;DR: Critical CSS is the minimum CSS to draw the above-the-fold content. Put it in a <style> in <head>, then load the rest without holding up rendering: <link rel="stylesheet" media="print" onload="this.media='all'"> or <link rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'">, plus a <noscript> as a backup. The two steps go together: inlining alone doesn't change the first paint (still 1,250 ms), deferring alone causes FOUC. In this example the two together bring the first paint from 1,250 ms to 350 ms. Keep the critical CSS small, HTML plus critical CSS around 14 KB, and extract it with a tool such as critical or Beasties.
⚡ A small suitcase for the first screen
Write:
<style>
.header { display: flex; height: 56px; padding: 0 16px; background: #1e293b; color: #fff; }
.hero { padding: 48px 16px; text-align: center; background: #fef3c7; }
</style>
Meaning: Critical CSS is the least CSS needed to draw the part that's visible right away (above the fold). Put it in a <style> in <head>, so the browser can use it at once, with no extra request.
⚡ Defer the rest
Guess first: the header and hero CSS is inlined, but the styles.css link is still plain. Does the first paint move?
No. It stays at 1,250 ms, because a plain link still holds up rendering. Only inlining plus a deferred link brings it down to 350 ms.
Write:
<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
Meaning: media="print" keeps the CSS from holding the screen, then onload switches it to all. <link> has no async or defer, so don't write them.
⚡ Keep it small
Meaning: HTML plus critical CSS around 14 KB, because a server's first send is usually only about 14 KB. Don't pick it by hand: extract it with a tool such as critical or Beasties.