How do I minify CSS?

Dark navy cover card with the title 'How do I minify CSS?' in white and a green accent bar

To minify CSS, run the stylesheet through a minifier that strips comments, whitespace and redundant characters. In a project with a build step, that means Lightning CSS, cssnano or esbuild, often already running inside your bundler. For a single file, paste it into an online CSS minifier and copy the result.

What minifying CSS removes

A CSS minifier deletes everything the browser doesn't need to apply the rules: comments, line breaks, indentation and the spaces around braces and colons. Better minifiers also rewrite values into shorter forms that mean the same thing.

The cssnano docs show one such rewrite: background: hsl(134, 50%, 50%); becomes background: #40bf5e; in its color minification example. Same color, fewer characters.

Here is a small rule before and after:

/* Card component */
.card {
  margin: 0px auto;
  color: #ffffff;
}
.card{margin:0 auto;color:#fff}

The selectors, properties and values the browser applies are unchanged.

Why minify CSS at all

Minified CSS downloads faster, and CSS speed matters more than most assets because it blocks rendering. The web.dev guide to resource loading says "CSS is a render-blocking resource", so the browser shows nothing until it has built the CSS Object Model. The same guide notes that minifying "reduces the file size of a CSS resource, making them quicker to download."

Minification is only one lever. Unused rules cost more than whitespace in many stylesheets. The guide recommends the Coverage tool in Chrome DevTools to find CSS the current page never uses, and deleting dead rules shrinks the file further than any minifier can.

Pick the method that fits your setup

The right method depends on whether you have a build step.

Your setup Best method Effort
Vite project Nothing to add; production builds already minify None
Other bundler or PostCSS pipeline cssnano as a PostCSS plugin One config file
Plain CSS files, no bundler Lightning CSS CLI in an npm script One command
A single file, a snippet, an email template Online CSS minifier Paste and copy
Relying on your CDN Move it into the build (see below) Small

Vite already minifies CSS

If your site builds with Vite, you probably don't need to do anything. The Vite build options page says "Vite uses Lightning CSS by default to minify CSS", and the build.cssMinify option defaults to 'lightningcss'.

Two settings change that. Setting build.cssMinify: 'esbuild' swaps the minifier. And CSS minification is off if build.minify is disabled for the client build, so check that flag if your output CSS still has comments in it.

Minify stylesheets from the command line with Lightning CSS

For plain stylesheets without a bundler, the Lightning CSS command-line tool is the shortest path. The Lightning CSS docs give the install and a build command:

npm install --save-dev lightningcss-cli
npx lightningcss --minify --bundle --targets ">= 0.25%" input.css -o output.css

--minify is the flag that does the work; --bundle and --targets are optional extras from the same example. Put the command in a build script in package.json so nobody minifies by hand.

Minify with PostCSS and cssnano

If your project already runs PostCSS (common with webpack setups and Tailwind), add cssnano as a plugin. The cssnano getting-started guide installs it with:

npm install cssnano postcss --save-dev

Then register it in postcss.config.js with the default preset:

module.exports = {
  plugins: [
    require('cssnano')({ preset: require('cssnano/presets/default') }),
  ],
};

Only enable it for production builds. Minified CSS in development makes DevTools harder to read for no gain.

Minify a single file online

For a one-off file, an online minifier is faster than setting up a tool. Paste the stylesheet into the CSS minifier, then copy the output or download it as a file. On OnlineViewer Dev the minifier runs as JavaScript in the page, and the tool code makes no network requests, so the CSS isn't uploaded.

The catch with any online minifier is that it's a manual step. Once someone edits the source, the minified copy is stale until someone pastes it again. For a stylesheet that changes, use a build script. The same trade-off applies to scripts; see how to minify JavaScript and how to minify HTML safely.

What about CDN minification?

CDN-side minification is no longer an option on Cloudflare. Cloudflare's API deprecation list shows the Auto Minify feature deprecated on August 5, 2024. Many older tutorials still tell you to switch it on in the dashboard.

Minifying in the build was always the better choice. The file you test locally is then the exact file your users get, with no edge rewrite in between.

Check the output after minifying

Most minified CSS works on the first try. Check these three things anyway.

  1. calc() spacing. The CSS Values spec says "whitespace is required on both sides of the + and - operators." A real minifier keeps calc(50% - 2em) intact. A hand-rolled regex that strips all spaces turns it into an invalid value, and the browser drops the whole declaration.
  2. License comments. cssnano removes ordinary comments, but its comment-removal docs note that "any special comments marked with ! are kept by default." Write licence headers as /*! ... */ if they must survive.
  3. The readable source. Keep the unminified file in version control and never edit the .min.css output. If you inherit a minified file with no source, run it through a formatter first; Prettier vs an online formatter compares the two routes.

FAQ

Does minifying CSS change how the page looks?

It shouldn't. A minifier removes comments and whitespace and shortens values without changing what the rules mean. If something does break, check calc() expressions and any hacks that rely on exact formatting.

Does Vite minify CSS automatically?

Yes. In a production build Vite minifies CSS with Lightning CSS by default. You can switch to esbuild with build.cssMinify, and CSS minification turns off if build.minify is disabled for the client build.

Can Cloudflare minify my CSS?

Not anymore. Cloudflare deprecated its Auto Minify feature on August 5, 2024, so minify in your build or before upload instead.

Should I edit the minified CSS file?

No. Keep the readable source in version control and treat the minified file as build output. Edit the source and minify again.

Sources