← All blog posts

CSS Minifier explained with worked examples

WhiteUI · A plain-English guide to the CSS Minifier

The CSS Minifier does four small things to your CSS: it removes comments, collapses whitespace, removes spaces around punctuation, and drops the semicolon before each closing brace. None of those changes the meaning of the CSS, which is why the result is safe to use. Three worked examples below show each of those steps on real CSS, with the exact byte counts before and after.

At a glance

Default sample: 68 bytes kept0 bytes52 bytes105 bytes105 bytes → 68 bytes, 37 saved
The default sample goes from 105 bytes to 68 bytes, saving 37 bytes.

Example 1: the default sample, 105 bytes to 68 bytes

The sample the tool opens with is two rules and a comment, 105 bytes long. The minifier removes the comment /* a note */, collapses the line breaks and the two-space indents, drops the spaces around the braces, colons and semicolons, and drops the semicolon before each closing brace. The output is .card{margin:0;padding:10px;color:red}.card .title{font-weight:bold}, which is 68 bytes. That is 37 bytes saved, or 35% smaller. The space between .card and .title stays, because it is part of the selector: it means a title inside a card.

Example 2: a button rule, 45 bytes to 33 bytes

Take a short rule with two declarations, written the way most people write CSS, one declaration per line with spaces after the colons:

.btn {
  color: blue;
  background: white;
}

That is 45 bytes. Minified, it becomes .btn{color:blue;background:white}, which is 33 bytes, so 12 bytes are saved, or 27% smaller. The saving comes from the line breaks, the indent spaces, the spaces after the colons, and the final semicolon before the closing brace. The two declarations are still there, in the same order, with the same values.

Example 3: a rule with a string and a calculation, 91 bytes to 75 bytes

Some CSS has text inside quotes, and that text must not change. This rule has a file name with a space in it, and a calc() value:

.hero {
  background: url("my photo.jpg");
  width: calc(100% - 20px);
  margin: 0 auto;
}

That is 91 bytes. Minified, it becomes .hero{background:url("my photo.jpg");width:calc(100% - 20px);margin:0 auto}, which is 75 bytes, so 16 bytes are saved, or 18% smaller. The space inside "my photo.jpg" survives, because the tool copies quoted strings untouched, and the spaces around the minus sign in calc(100% - 20px) survive too, because a minus sign is not one of the punctuation marks the tool strips spaces around. Only the spaces that change nothing are removed.

What never changes

Across all three examples, the same things survive: selector names, property names, values, quoted strings, and the space in a descendant selector such as .card .title. What goes is only ever comments, extra whitespace, spaces around punctuation, and the semicolon before a closing brace. If you compare the original and the minified version side by side, every word that affects how the page looks is still there.

The common mistake

People look at the saved figure and assume a bigger saving means a better result. It does not: the saving depends on how the CSS was written, not on how good it is. A stylesheet that was already written tightly, with short lines and no comments, saves little, while a stylesheet full of comments and blank lines saves a lot. Both minify to valid CSS, and the percentage only tells you how much dead weight the original was carrying.

Frequently asked questions

What does the CSS Minifier remove?

Comments, extra whitespace, spaces around punctuation such as braces and colons, and the semicolon before each closing brace.

How much does the default sample shrink?

From 105 bytes to 68 bytes, saving 37 bytes, which is 35% smaller.

Does minifying change quoted text?

No. Strings in quotes are copied untouched, so "my photo.jpg" keeps its space.

Does minifying break calc()?

No. The spaces around the minus sign in calc(100% - 20px) survive, because a minus sign is not one of the punctuation marks the tool strips spaces around.

More guides on the CSS Minifier

Try it yourself

Try the CSS Minifier — free, in your browser.

Open the CSS Minifier