Critical CSS Explained: Faster First Paint
Inlining the above-the-fold CSS removes a render-blocking request. Learn how to extract it, inline it, and load the rest asynchronously.
By Velloreth
Why a stylesheet blocks
A stylesheet in the head blocks the render.
<head>
<link rel="stylesheet" href="/styles.css">
</head>
The browser must download and parse the CSS before it can paint, because the CSS determines the layout. Until it has the CSS, it cannot know where anything goes.
On a fast connection this is invisible. On a slow one, the user sees a blank page for the duration of the download.
The first contentful paint is the moment the first content appears, and it is a Core Web Vital. A render-blocking stylesheet delays it.
The technique
Inline the critical CSS in the head, and load the rest asynchronously.
<head>
<style>
/* The above-the-fold rules, inlined */
body { margin: 0; font-family: system-ui, sans-serif; }
header { display: flex; padding: 1rem; }
.hero { min-height: 60vh; }
</style>
<link rel="preload" href="/styles.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles.css"></noscript>
</head>
The inlined CSS renders the first screen immediately.
The preload with an onload loads the full stylesheet without blocking, and switches it to a stylesheet once it arrives.
The noscript fallback covers a browser with JavaScript disabled.
The media="print" trick is the older alternative:
<link rel="stylesheet" href="/styles.css" media="print" onload="this.media='all'">
A print stylesheet does not block the render, and the onload switches it to all.
What counts as critical
The content in the initial viewport, at the most common screen size.
The base styles: the reset, the font, the colours, the container.
The header and the hero, which are usually above the fold.
Not the footer, the below-the-fold sections, or a modal.
The size budget: keep the inlined CSS under about 14KB, which fits in the first round trip of a TCP connection. Beyond that, the inlined CSS itself becomes a delay.
Extracting it
By hand does not scale. A change to the layout invalidates the extraction, and the maintenance burden grows.
A build tool is the practical answer.
npx critical index.html --inline --minify > index-critical.html
critical renders the page in a headless browser, determines which rules apply above the fold, and inlines them.
critters is a webpack plugin that does the same during a build.
A framework plugin is the cleanest option: Next.js, Nuxt, and Astro have an option or a plugin for it.
The build step must run on the real page, with the real CSS, or the extraction is wrong.
The trade-offs
The benefit is a faster first paint, which improves the first contentful paint and the largest contentful paint.
The cost is a build step, and a risk of a flash of unstyled content if the critical CSS is incomplete.
The benefit is smaller with HTTP/2, because a single stylesheet is one request and the connection is multiplexed. It is still real on a slow connection.
The benefit is smaller for a small stylesheet. If the whole CSS is 10KB, inlining all of it is simpler than extracting a subset.
The honest assessment: measure first. If the first contentful paint is already good, the complexity is not worth it.
A flash of unstyled content
The symptom: the page renders with the critical CSS, then jumps when the full stylesheet arrives.
The cause: the critical CSS is missing a rule that the full stylesheet has, so the layout changes.
The fix: include every rule that affects the above-the-fold layout, and test on a throttled connection.
A common miss is a font-face rule, so the text renders in a fallback font and then swaps. Include the font-face in the critical CSS, or use font-display: swap and accept the swap.
Measuring
The first contentful paint is the metric to watch.
The largest contentful paint is the second, because the hero image or the heading is often the largest element.
Test on a throttled connection, because a fast connection hides the problem.
Use Lighthouse or the Performance panel, and compare before and after.
A field measurement (from real users) is more reliable than a lab one, because it includes the real network conditions.
When it is worth the complexity
Critical CSS is a real optimisation and it is not free, so the decision should follow a measurement rather than a principle. If the first contentful paint is already under a second on a throttled connection, the build step and the maintenance burden buy very little, and the simpler architecture is worth more. The technique pays off when the stylesheet is large, the connection is slow, and the first paint is visibly delayed, which is the case for a content site with a heavy design system. A useful middle ground is to inline the whole stylesheet when it is small, which removes the render-blocking request without the extraction step and the risk of an incomplete subset. The honest test is to throttle the connection, load the page, and watch whether the content appears before the stylesheet arrives; if it does not, the optimisation is worth considering, and if it does, the complexity is not.
Practical guidance
- Inline the above-the-fold CSS.
- Load the rest asynchronously.
- Add a
noscriptfallback. - Keep the inlined CSS under 14KB.
- Use a build tool to extract it.
- Include the font-face in the critical CSS.
- Test on a throttled connection.
- Measure the first contentful paint.
- Skip it if the stylesheet is already small.
For the related guides, see core-web-vitals-guide and improve-inp-core-web-vitals-static-site. For the broader context, http-caching-headers and how-cdns-actually-work.
Quotable summary
A stylesheet in the head blocks the render, because the browser must build the CSS object model before it can paint, so the first contentful paint waits for the download. Critical CSS is the subset needed for the above-the-fold content, inlined in the head, with the rest loaded asynchronously so it does not block the first paint. A build step is the practical way to extract it, because doing it by hand does not scale and a change to the layout invalidates the extraction. Keep the inlined CSS under about 14KB so it fits in the first round trip, include the font-face rule to avoid a font swap, and measure the first contentful paint on a throttled connection before deciding the complexity is worth it.