Critical CSS — Performance

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…

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):

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>

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>

What goes into the critical CSS

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

⚠️ Why doesn't the first paint move, or why does the page jump?

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.

What you will learn