Critical Resources in HTML
A page can contain hundreds of files, but only a handful decide how fast it feels. Those few are the critical resources: the files the browser must have before it can show something useful.
Performance work is largely a hunt for them. Once you know which files are critical, you can make them small, find them early, and stop everything else from getting in their way. This lesson shows you how to find them and how to shorten the path they take.
What You Will Learnβ
- What makes a resource critical
- What the critical rendering path is
- Why chains of files are slower than the same files side by side
- Which resources block painting, and how to unblock them
- Techniques: flattening, inlining,
media,defer, fonts, and preload - How to find your own critical resources in DevTools
The Big Ideaβ
Think of the first screen as a meal, and the critical resources as the ingredients without which you cannot serve anything. Everything else is a side dish: it can arrive later.
- HTML Code
- Chain Lab
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The Bread Bin</title>
<!-- Open the connection to the font host early -->
<link rel="preconnect" href="https://fonts.example.com" crossorigin>
<!-- A font the headline needs, found early instead of late -->
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
<!-- Only the CSS the first screen needs, inside the page -->
<style>
body { margin: 0; font-family: system-ui, sans-serif; }
.hero { min-height: 60vh; background: #f3e9dc; }
</style>
<!-- The rest of the CSS: does not block the first paint -->
<link rel="stylesheet" href="/css/rest.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="/css/rest.css"></noscript>
<!-- Downloads now, runs after the page is read -->
<script src="/js/app.js" defer></script>
</head>
The page loads site.css, which uses @import for fonts.css, which names a font file. site.css also sets the hero image as a background. A plain main.js sits in the head.
| Moment | Now | With no fixes |
|---|---|---|
| First paint (FCP) | 620 ms | 620 ms |
| Custom font ready | 920 ms | 920 ms |
| Hero image shown (LCP) | 920 ms | 920 ms |
Longest chain of files that must arrive one after another: 4 steps. Shorter chains are faster, especially on slow connections.
This is a simplified model with fixed sizes and times. It assumes the critical CSS inlined in the page is enough for the first screen. Real results depend on your files and network, so measure your own.
In the lab, tick the fixes one by one and read the three moments. Some fixes help the first paint, some help the font, and some help the hero image. Which single fix helps the most? Which ones help only in combination?
What Makes a Resource Critical?β
Ask three questions about any file:
- Does the browser need it before it can paint? CSS in the head and plain scripts do. These are render-blocking.
- Is it part of the first screen? The main image, the headline font, and the logo are.
- Does something else wait for it? A stylesheet that imports another one makes the second file critical too.
A file that answers "no" to all three can wait, or be lazy, deferred, or removed.
The Critical Rendering Pathβ
The critical rendering path is the sequence of steps from "the browser got the HTML" to "pixels on the screen":
- Read the HTML and build the page structure.
- Fetch and read the CSS, then work out how everything looks.
- Run any blocking scripts.
- Lay out the page and paint it.
Every file in those steps is on the critical path. The length of the path matters in two ways: how many bytes it carries, and how many separate steps, one after another, it needs.
Chains Are the Hidden Costβ
Files that can be fetched side by side cost roughly the time of the slowest one. Files that must be fetched one after another cost the sum of their times, plus a delay for each hand-off.
Here is a typical chain:
index.html -> site.css -> fonts.css (via @import) -> inter.woff2
The browser cannot know about fonts.css until it has read site.css, and cannot know about the font file until it has read fonts.css. Each link in the chain adds a full round trip to the server. On a slow or distant connection, round trips are the most expensive thing there is.
The cure is to make chains shorter and wider: get files discovered in the HTML, where the browser finds them immediately and can fetch them in parallel.
What Blocks, and How to Unblockβ
| Resource | What it blocks | How to unblock |
|---|---|---|
| CSS in the head | The first paint | Keep it small, inline the critical part, split out the rest |
| A plain script | Reading the rest of the HTML | Use defer or async |
@import in CSS | The CSS, and what depends on it | Use a link element instead |
| A web font | Text in that font, or it swaps late | Preload, host it close, and use font-display |
| A CSS background image | Found only after the CSS is read | Preload it, or use an img element |
| A third-party stylesheet | The first paint, if it is in the head | Host it yourself, or load it later |
Seven Techniquesβ
1. Flatten the chainβ
Replace @import with link elements in the HTML, so the browser finds every file at once.
<!-- Before: site.css contains @import url("fonts.css"); -->
<link rel="stylesheet" href="/css/site.css">
<!-- After: both are found immediately -->
<link rel="stylesheet" href="/css/fonts.css">
<link rel="stylesheet" href="/css/site.css">
2. Inline the critical CSSβ
Put the few rules the first screen needs directly inside a style element, so the first paint does not wait for a CSS file. Keep it small: a commonly quoted rule of thumb is to keep the whole first response within roughly 14 KB compressed, so it arrives in the first round trip, but treat that as a guide and measure. Remember that inline CSS is repeated on every page and cannot be cached on its own, so it is a trade-off, and works best for a short, stable set of rules. Many build tools can extract critical CSS for you.
3. Do not let unneeded CSS blockβ
A stylesheet with a media value that does not match the current screen is not render-blocking. Browsers still download it, usually at a lower priority.
<link rel="stylesheet" href="/css/print.css" media="print">
<link rel="stylesheet" href="/css/wide.css" media="(min-width: 1200px)">
The "print then switch to all" trick in the code above uses the same idea to load the rest of your CSS without blocking. It needs JavaScript (hence the noscript fallback), and an inline onload may conflict with a strict content security policy. It can also cause a brief flash if the rest of the CSS styles things on the first screen, so check carefully.
4. Defer scriptsβ
Use defer for your own scripts and async for independent ones, as covered in Script Loading, Defer, and Async.
5. Treat fonts with careβ
A font file is discovered late, because it is named inside CSS. Help the browser:
- Preload the one or two files the headline needs, with
crossorigin(see Preload). - Preconnect if the font comes from another host (see Preconnect).
- Use
font-display: swapin your@font-face, so text shows in a fallback font at once and swaps when the font arrives. There is a short change of look, so test how it feels. - Use fewer files and weights, and subset large fonts.
- Consider hosting fonts yourself, which removes a third-party connection from the critical path.
6. Preload what is found lateβ
For the hero image set in CSS, or any critical file the browser discovers late, a preload starts the download earlier. Use it sparingly.
7. Remove what you do not needβ
The fastest critical resource is the one that is not there. Delete unused CSS, unused fonts, and old scripts.
Find Your Own Critical Resourcesβ
Try the quiz first, to sharpen your instincts.
For the first screen of a bakery page, which of these is critical, and which can wait?
Then find the real ones on your own pages:
- Open DevTools, then Network, and reload on a throttled connection.
- Look at the Waterfall, and find the files that start late. A file that starts only after another finishes is part of a chain.
- Click a file and open its Initiator to see what caused it to load.
- Check the Priority column. High priority files are the ones the browser thinks are critical.
- Run Lighthouse and read the suggestions about render-blocking requests and chained critical requests. Names of audits change over time, so read the descriptions.
- Open the Performance panel, and watch which files stand between navigation and the first paint.
- Use the Coverage tool to see how much CSS and JavaScript the page does not use.
Best Practicesβ
- Know your critical resources. Write the list down for each key page.
- Keep the list short, and each file small.
- Shorten chains. Get files discovered in the HTML.
- Never use
@importfor important CSS. - Defer scripts, and keep third-party code off the critical path.
- Give fonts special care: preload, host close, and use
font-display. - Preload only what is late-found and important.
- Measure on a slow connection, since chains hurt most there.
Quick Checklistβ
- The critical resources for the first screen are known
- No
@importfor important CSS - CSS in the head is small, with the critical part inlined if it helps
- Non-critical CSS does not block the first paint
- Scripts use
deferorasync - Fonts are few, preloaded if critical, and use
font-display - The hero image is found early, and not lazy
- No third-party stylesheet in the head that you do not control
- The waterfall shows no long chains
Security and Privacy Notesβ
- Critical third-party files are a single point of failure. If a font or stylesheet host is slow or down, your first paint waits for it. Hosting critical files yourself is both faster and more reliable.
- Third-party hosts see your visitors. Fonts and styles from another host share the visitor's address and some details with it. Self-hosting avoids this. If you must use one, load it in line with the rules that apply to you. This is not legal advice.
- Inline CSS and inline handlers can clash with a content security policy. A strict policy blocks inline styles and
onloadattributes unless you allow them with a nonce or hash. Plan for this if you inline critical CSS. - Use integrity hashes for fixed third-party files. The
integrityattribute lets the browser reject a file that has been altered. - Preloads contact hosts early. Do not preload from hosts that need consent first.
Global Audience Notesβ
- Round trips cost more where latency is high. Visitors far from your servers, on mobile networks, or on satellite links pay for every step in a chain. Short chains and a nearby CDN help them most.
- Self-hosted fonts save a connection to another country's servers, and avoid problems where a font host is slow or blocked.
- Fonts for some scripts are large. Chinese, Japanese, and Korean fonts can be very big. Subset them, use system fonts for body text, or keep web fonts for headings only.
- Test on slow connections and modest phones. What is critical on a fast laptop is not always the same as on a slow phone.
- Keep the page useful without the extras: text should be readable even before the custom font arrives.
Common Mistakes to Avoidβ
@importchains for important CSS.- A huge CSS file where most rules are unused.
- Plain scripts in the head.
- A web font discovered late, with no preload.
- A hero image hidden in CSS with no preload.
- Third-party stylesheets in the head that you do not control.
- Inlining far too much CSS, so every page is heavy.
- Preloading everything, so nothing stands out.
- Splitting CSS carelessly, so the first screen flashes unstyled.
- Only testing on a fast connection.
Practice Timeβ
- In the Chain Lab, tick one fix at a time. Which helps the first paint most?
- Which fix helps the hero image most? Which helps the font most?
- Tick them all. How long is the chain now?
- Take the critical-or-not quiz. Which answer surprised you?
- Open a page you built, and sort the Network waterfall by start time. Find a chain.
- Replace an
@importwith alink, and compare. - Add
media="print"to a print stylesheet. - Bonus: run Lighthouse, read the suggestions about chained requests, and fix the longest chain.
Quick Recapβ
Click a question to check your answer.
1. What is a critical resource?
A file the browser needs before it can show something useful: render-blocking CSS and scripts, plus the files that make up the first screen, such as the main image and headline font.
2. Why are chains slow?
Each file in a chain can only be discovered after the one before it arrives, so you pay for each round trip one after another, instead of fetching them side by side.
3. How do you flatten a chain?
Make the browser discover every file in the HTML, for example by replacing @import with link elements, or by preloading late-found files.
4. What does inlining critical CSS do, and what is its cost?
It lets the first paint happen without waiting for a CSS file. The cost is that the CSS is repeated on every page and cannot be cached on its own, so keep it small.
5. How can a stylesheet avoid blocking rendering?
Give it a media value that does not match the current screen, such as print. It still downloads, but does not hold up the first paint.
6. How do you find your critical resources?
Use the Network waterfall, the Initiator and Priority columns, Lighthouse's suggestions, and the Performance panel to see which files stand between navigation and the first paint.
Key Takeawaysβ
- A few files decide how fast the page feels. Find them first.
- Chains cost the sum of their steps. Make them shorter and wider.
- CSS in the head and plain scripts block painting. Keep CSS small, and defer scripts.
- Fonts and CSS backgrounds are found late. Preload them, or move them into the HTML.
- Host critical files yourself where you can, for speed, reliability, and privacy.
- Remove what you do not need.
- Measure on slow connections, and re-check after every change.
Next up: Core Web Vitals, where you will learn the three numbers that measure how all of this feels to real visitors.