Sub topic 11
Performance: render blocking and caching
This chapter assumes you can write the three ways of attaching CSS (the style attribute, the style element, and a link element) and that you know how a browser requests a file by URL. It covers two performance effects: when the browser waits for CSS before it shows anything, and when it can reuse a stylesheet it already downloaded.
Why CSS blocks rendering
A browser builds a model of the page's styles from every stylesheet it finds. It will not paint the page until that model is complete. Painting earlier would show unstyled content that then jumps when the CSS arrives. For this reason, CSS is called render blocking.
A link rel="stylesheet" in the head blocks rendering until the file has downloaded and been parsed. It also delays any script that follows it, because scripts may read styles.
A style element blocks too, but it costs almost nothing. The rules are already in the HTML, so there is no extra request.
Cost of each method
| Method | Extra request | Blocks first paint | Cached separately |
|---|---|---|---|
| `style` attribute | No | No extra delay | No, it is part of the HTML |
| `style` element | No | No extra delay | No, it is part of the HTML |
| `link` to a `.css` file | Yes, on first visit | Yes, until loaded | Yes |
Inline and internal styles avoid a network round trip. The cost is that the CSS travels with every HTML response. If a site has 50 pages that each embed the same 20 KB of rules, a visitor downloads those 20 KB 50 times.
An external file is the reverse. The first page pays for one extra request. Every later page reuses the cached copy, so only the HTML is transferred.
Caching an external stylesheet
The server controls how long the browser may reuse a file. It does this with a response header:
Cache-Control: public, max-age=31536000, immutableThis tells the browser to keep the file for a year and not to revalidate it. That works well only if the file's URL changes whenever its content changes. If you edit site.css but keep the same URL, visitors keep seeing the old styles until their cache expires.
The usual fix is to put a version or content hash in the filename:
<link rel="stylesheet" href="css/site.3f9a1c.css">Build tools generate the hash for you. Without a build tool, a query string such as site.css?v=12 also works, because the browser treats it as a different URL.
Limiting what blocks
You can tell the browser that a stylesheet applies only in some situations. The browser still downloads it, but a non-matching stylesheet does not block the first paint:
<link rel="stylesheet" href="css/site.css">
<link rel="stylesheet" href="css/print.css" media="print">
<link rel="stylesheet" href="css/wide.css" media="(min-width: 1200px)">On a narrow screen, wide.css does not hold up rendering. If the window is later resized, the browser applies it.
A common technique for fast pages is to inline a small amount of critical CSS, the rules needed for the visible top of the page. The full stylesheet is then linked as usual. The first paint needs no extra request, and later pages still benefit from the cached file.
<head>
<meta charset="utf-8">
<title>Critical CSS</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; }
header { background: #123; color: white; padding: 1rem; }
</style>
<link rel="stylesheet" href="css/site.3f9a1c.css">
</head>Common mistakes
- Chaining
@import. A file loaded with@importis discovered only after the first file has been downloaded and parsed. Each level adds a round trip. Separatelinkelements let the browser fetch files in parallel. - Inlining everything. Large embedded CSS makes every HTML response heavier and can never be cached on its own.
- Linking many small files. Each file is a request. Over HTTP/2 this costs less than it used to, but combining files is still often worthwhile.
- Editing a cached file without changing its URL. The change appears to have no effect for returning visitors.
Exercise
A site has 30 pages. All of them embed the same 15 KB of CSS in a style element. A designer proposes moving it to a linked file named site.css. Describe one benefit and one cost of this change, and what you should do to make sure a later edit to the CSS reaches returning visitors.
Show answer
Benefit: after the first page, the browser reuses the cached file, so each later page downloads 15 KB less. Cost: the first visit needs an extra request, and the page cannot paint until site.css has downloaded. To make later edits reach returning visitors, change the file's URL whenever its content changes, for example with a hash in the filename (site.3f9a1c.css) or a version query string, so the browser does not serve the old cached copy.