What minification actually removes
CSS minification strips what the browser does not need: comments, indentation, line breaks, the final semicolon in a block, and empty rules. How much that saves depends on how the stylesheet was written: run through this minifier, four open-source stylesheets lost between 15% and 63% of their bytes, and between 10% and 58% after gzip at level 9. Comment-heavy files gain the most.
Where naive minifiers break stylesheets
The common shortcut is a regular expression that deletes whitespace around {, }, :, ; and ,. It fails in four places that appear in real code:
- Inside strings —
content: "hello, world"loses the space after the comma, changing what the page renders. - In
calc()—calc(100% - 2rem)becomescalc(100%-2rem), which is invalid and drops the declaration. - In selectors —
a :hoverbecomesa:hover, a completely different selector. - In at-rules —
@media (min-width: 600px)becomes@media(min-width:600px), which is not valid.
This minifier tokenises the stylesheet instead, so those cases survive. Beautify reverses the process when you need to read minified CSS.
For the markup around your styles, see the HTML beautifier, and for scripts the JavaScript formatter.