SpunkArt
Front-end and everyday web

What CSS minification removes, and what it must not touch

Minifying CSS means rewriting a stylesheet into the smallest text that the browser reads the same way. The rules look identical to the browser, but the file costs fewer bytes to download.

A before-and-after sample

Here is a small readable stylesheet of 162 bytes:

/* Card component */
.card {
  margin: 0px 0px 16px 0px;
  padding: 12px;
  color: #ffffff;
  background-color: #333333;
}

.card .title {
  font-weight: bold;
}

A typical minifier turns it into one line of 97 bytes:

.card{margin:0 0 16px;padding:12px;color:#fff;background-color:#333}.card .title{font-weight:700}

That is 65 bytes less, a reduction of about 40% (1 − 97 ÷ 162 = 0.401). Real stylesheets save a different amount depending on how many comments and how much indentation they contain.

The rewrites, one by one

  1. Comments go. /* Card component */ is for humans only. Some tools keep comments that start with /*!, which is a convention for licence notices.
  2. Whitespace goes around braces, colons and semicolons, and line breaks disappear.
  3. The last semicolon in a block goes. CSS does not need a semicolon before }.
  4. Zero units go. 0px becomes 0 because zero is zero in any length unit.
  5. Shorthands shrink. 0 0 16px 0 becomes 0 0 16px, because a missing fourth value copies the second one.
  6. Colours shrink. #ffffff becomes #fff when each pair of hex digits repeats.
  7. Keywords become numbers when shorter: bold is the same as 700.

Where minifying goes wrong

Not every space is decoration. These are the places a careless tool, or a careless hand edit, breaks a rule:

  • Inside calc(). calc(100% - 10px) needs the spaces around - and +. Without them the browser reads -10px as a negative number and drops the whole declaration.
  • Descendant selectors. The space in .card .title means “inside”. Removing it gives .card.title, which means one element with both classes.
  • Strings and URLs. Text inside content: "a b" or a quoted URL must stay byte for byte.
  • Rule order. Merging two rules that are far apart can change which one wins the cascade. Good tools only merge when the order cannot matter.
  • Hacks for old browsers. A rewrite can drop a deliberate duplicate declaration, such as a fallback colour written before a newer colour syntax. Check that fallbacks survive.

Minify in a build step, not by hand

Keep the readable file in your project and generate the minified copy automatically. Common approaches:

  • A bundler such as Vite, webpack or esbuild minifies CSS in its production build.
  • PostCSS with the cssnano plugin, or the Lightning CSS tool, can run on their own from the command line.
  • Many static hosts and CDNs offer automatic minification as a setting.

Turn on a source map as well. It lets the DevTools Styles panel point to the original line in the readable file even though the browser loaded the one-line version.

Minification versus compression

A server usually sends text files with gzip or Brotli compression. Compression already squeezes out repeated whitespace very well, so the gain from minifying shrinks after compression. Both still help, and they work together: minify first, then let the server compress the result. You can see the transferred size and the real size side by side in the DevTools Network panel.

Questions

Does minified CSS behave any differently?

It should not. A correct minifier only makes changes that give the same computed styles. If a page looks different after minifying, compare the two files around calc(), selectors and strings first.

Should I commit the minified file?

Usually not. Commit the source and let the build create the minified output. That keeps code reviews readable and avoids two copies drifting apart.

Is unused-CSS removal the same thing?

No. Minifying rewrites the rules you have. Purging removes selectors that no page uses, which needs a tool that scans your HTML and scripts and can remove a rule you still need if it misses a class added at run time.