CSS blocks rendering: the browser won't paint a page until it has downloaded and parsed the stylesheets in the <head>. Smaller CSS reaches the browser faster, and minification is the easiest first step. It's not the biggest one, though.
What minification changes
A whitespace stripper removes spaces, line breaks and comments. A real minifier parses the CSS and also rewrites it into shorter equivalents:
/* Before */
.card {
margin: 0px 0px 0px 0px;
color: #ffffff;
background-color: rgb(255, 0, 0);
font-weight: bold;
}
.card { padding: 8px; }
/* After */
.card{color:#fff;background-color:red;margin:0;padding:8px;font-weight:700}
Typical transformations:
- Colors in their shortest form (
#ffffff→#fff,rgb(255,0,0)→red). - Zero values without units, and shorthand merging (
margin: 0px 0px 0px 0px→margin:0). - Duplicate selectors merged, overridden declarations removed.
bold→700, and redundant semicolons dropped.
Because it understands the syntax, a parser-based minifier also catches errors, like a missing brace that would otherwise swallow the rest of the stylesheet.
Minify in your build
You probably already have a minifier. Next.js, Vite, Parcel and most frameworks minify CSS in production builds by default. To check, look at the CSS files in your build output: they should be on one or a few long lines.
Lightning CSS
Fast and written in Rust. Besides minifying, it can add vendor prefixes and lower modern syntax for older browsers:
npx lightningcss --minify --bundle --targets '>= 0.25%' src/styles.css -o dist/styles.css
import { transform, browserslistToTargets } from "lightningcss";
import browserslist from "browserslist";
const { code } = transform({
filename: "styles.css",
code: Buffer.from(css),
minify: true,
targets: browserslistToTargets(browserslist(">= 0.25%")),
});
Vite
Vite uses esbuild to minify CSS by default; set build.cssMinify: "lightningcss" to switch.
PostCSS and cssnano
If your pipeline already uses PostCSS:
// postcss.config.js
module.exports = { plugins: [require("autoprefixer"), require("cssnano")({ preset: "default" })] };
esbuild
npx esbuild src/styles.css --bundle --minify --outfile=dist/styles.css
How much it saves
Minification often shrinks hand-written, well-commented CSS noticeably before compression. After gzip or Brotli, the difference is much smaller, because compression already handles repeated whitespace well. Measure your own files:
for f in src/styles.css dist/styles.css; do
printf '%s raw=%s gzip=%s\n' "$f" "$(wc -c < $f)" "$(gzip -c $f | wc -c)"
done
The same pattern holds for JSON and JavaScript; the JSON payload guide walks through measuring it.
Bigger wins than minifying
Remove unused CSS
Frameworks and UI kits ship far more CSS than one page uses. Tools that compare your CSS against your templates remove the rest:
- Tailwind CSS generates only the classes found in your source files.
- PurgeCSS works with any CSS and a list of content files.
- Chrome DevTools' Coverage panel shows how much of each stylesheet a page actually used.
Be careful with classes created dynamically in JavaScript; safelist them so they aren't removed.
Split and inline critical CSS
Inline the small amount of CSS needed for the first screen in the <head>, and load the rest without blocking rendering. This helps most on content sites with large stylesheets.
Avoid @import chains
@import inside CSS makes the browser download files one after another. Bundle them at build time instead.
Cache aggressively
Serve CSS with a content hash in the file name (styles.3f9a1c.css) and Cache-Control: public, max-age=31536000, immutable. Returning visitors then download nothing at all.
Mistakes to avoid
- Minifying by hand or with regexes. They break strings, URLs and
calc()expressions that need spaces:calc(100% - 2rem)is invalid without the spaces around-. - Editing minified files. Changes are lost on the next build.
- Comments that must stay. License comments starting with
/*!are kept by most minifiers; others are removed. - Debugging production CSS without source maps. Generate them so DevTools points at real file names and lines.
JavaScript benefits from minification more than CSS does, because a minifier can also rename variables; see JavaScript minification with Terser.