CSS Minifier formula — how it actually works
There is no mystery inside the CSS Minifier: it applies four rules to your CSS, in order, and the output is whatever is left after those four rules have run. The rules are short enough to write down, and the worked example below runs them on the tool’s own default sample so you can check each step yourself.
1. Remove every /* ... */ comment
2. Collapse each run of whitespace to one space
3. Remove spaces around { } : ; , > + ~ ( ) [ ]
4. Drop the ; before each }
Strings in quotes are copied untouchedAt a glance
Rule 1: comments come out
Anything between /* and */ is a comment, and the tool deletes it whole. Comments are notes for people, and the browser ignores them, so removing them changes nothing about how the page looks. In the default sample, the comment /* a note */ disappears at this step, along with the line it sat on.
Rule 2: whitespace collapses
Line breaks, tabs and indent spaces are all whitespace, and the tool collapses each run of them to a single space. A rule written over five lines becomes one line, and a two-space indent becomes nothing at all once rule 3 runs. The only whitespace that survives is a single space where a space is needed, such as the space between .card and .title in a descendant selector.
Rule 3: spaces around punctuation go
The tool removes spaces around the punctuation marks { } : ; , > + ~ ( ) [ ]. A space after a colon, such as color: red, becomes color:red, and a space before a brace, such as .card {, becomes .card{. None of those spaces affects the meaning: the colon still separates the property from its value, and the brace still opens the block. Spaces inside quoted strings are never touched, because the tool copies strings character by character before any of these rules run.
Rule 4: the last semicolon goes
A semicolon separates one declaration from the next, so the semicolon before a closing brace separates a declaration from nothing. Dropping it saves one byte per block and changes nothing, which is why the minified sample ends each block with a value and then } rather than ;}. With two blocks in the sample, this rule alone saves 2 bytes.
Worked example: the default sample, step by step
The sample is 105 bytes: two rules and a comment. Rule 1 removes the comment. Rule 2 turns the line breaks and indents into single spaces. Rule 3 removes the spaces around the braces, colons and semicolons, leaving .card{margin:0;padding:10px;color:red;}.card .title{font-weight:bold;}. Rule 4 drops the two semicolons before the closing braces. The result 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, and every step can be checked by hand against the four rules.
The common mistake
People assume the tool rewrites their CSS, for example by shortening colour names or merging rules. It does not: it only removes characters, it never changes a value, renames a selector, or reorders a declaration. If your original CSS says color: red, the minified CSS says color:red, with the same word red. Anything fancier than removing characters, such as shortening #ffffff to #fff, is a different tool’s job, not this one’s.
Frequently asked questions
What are the four rules the CSS Minifier applies?
Remove /* */ comments, collapse whitespace, remove spaces around punctuation, and drop 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.
Why is the last semicolon removed?
A semicolon before a closing brace separates a declaration from nothing, so dropping it saves one byte per block with no change in meaning.
Does the tool change my values or selectors?
No. It only removes characters. Selector names, property names and values stay exactly as written.
More guides on the CSS Minifier
- How to use the CSS Minifier — step by step
- CSS Minifier explained with worked examples
- CSS Minifier for beginners — start here
- Common mistakes people make with the CSS Minifier
- CSS Minifier vs doing it by hand — which is faster?
- CSS Minifier — the complete guide
- How to get an accurate result from the CSS Minifier
- CSS Minifier for students — with examples
- CSS Minifier for homeowners and DIYers
Try the CSS Minifier — free, in your browser.