Sub topic 13
Bundling, splitting and delivering CSS
This chapter assumes you can write BEM components, use @layer, and run an npm build. It covers how the CSS you organised in earlier chapters reaches the browser.
What the browser needs
A browser treats a stylesheet in the <head> as render-blocking. It will not paint the page until that CSS has downloaded and been parsed. Delivery therefore has two goals:
- Send as little CSS as the current page needs.
- Cache what you send for as long as possible.
Bundling and splitting pull in opposite directions. One large bundle caches well and is requested once, but it carries rules the page never uses. Many small files send only what is needed, but they add requests and can change the cascade order.
Bundle, then split
A bundler follows your @import statements and import "./x.css" lines and writes out a smaller set of files. Most tools can also split by route or by lazily loaded component. Vite does this by default. Each dynamically imported chunk gets its own CSS file, which loads together with the chunk's JavaScript.
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
build: {
cssCodeSplit: true,
cssMinify: "lightningcss",
},
});cssMinify: "lightningcss" needs the lightningcss package installed (npm install -D lightningcss). Minification removes whitespace and comments and merges duplicate declarations.
A reasonable split is:
- A shared file with tokens, reset, base styles and common components.
- One file per route or large feature.
- A print stylesheet, if you have one.
Keep the cascade stable across files
Splitting is where cascade bugs appear. When two files load in a different order on different pages, rules of equal specificity swap winners. Cascade layers fix this, provided the layer order is declared before any layered rule is parsed.
/* layers.css, always included first in the shared bundle */
@layer reset, base, components, utilities;Every other file can then write @layer components { ... } and it lands in the right slot, whatever order the files arrive in. Unlayered CSS beats all layers, so keep third-party and legacy rules inside a named layer. Otherwise they override your utilities.
Load non-critical CSS without blocking
A stylesheet whose media attribute does not match the current device is downloaded at low priority and does not block rendering. Content hashes in the file names let you cache the files for a year.
<link rel="stylesheet" href="/assets/shared.3f9a1c.css">
<link rel="stylesheet" href="/assets/print.7b2d90.css" media="print">The hash changes only when the file content changes. Serve these files with Cache-Control: public, max-age=31536000, immutable. Do not cache the HTML page that references them for as long.
For above-the-fold speed, some teams inline a small amount of "critical" CSS in a <style> element and load the rest normally. This helps first paint. It also duplicates bytes and adds a build step, so measure before adopting it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Styles differ between pages | Chunks load in different orders | Declare `@layer` order in the first file |
| A flash of unstyled content | CSS injected late by JavaScript | Extract CSS to files at build time |
| Same rules in several chunks | A shared module is imported by many routes | Configure a shared chunk, or review imports |
| Users see old styles after deploy | File names have no hash | Add content hashes to file names |
| Large CSS on every page | One bundle for the whole site | Split by route and measure coverage |
Exercise
Your site has a shared bundle and per-route CSS files. On one route, a .p-0 utility stops overriding .card, but only when that route is opened directly. What is the most likely cause, and how do you fix it?
Show answer
The route's CSS file is parsed before the shared file that declares the layer order, or the @layer order statement is missing from the first file loaded. Without the order declared up front, layers are ordered by first appearance, so components can end up after utilities. Put @layer reset, base, components, utilities; in a file that always loads first, and confirm that both .card and .p-0 are inside their named layers.