How to Minify CSS: A Step-by-Step Guide
Last updated: · 3 min read
Stylesheets are written with indentation and comments so that people can read them during development, but in production those spaces and notes are just extra bytes. The CSS Minifier removes comments and unnecessary whitespace, drops the last semicolon in each block and writes the rules on one line. Strings inside quotation marks, such as the text of a content property, are kept exactly as they are. The tool runs in your browser, so your stylesheet is never sent to a server.
Formula
1. Split the text into tokens according to CSS syntax: selector, declaration, string, comment and curly brace. 2. Delete comments written as /* ... */ entirely. 3. Keep the characters inside a string, including spaces between quotation marks, exactly as they are. 4. Remove the spaces around punctuation such as braces, semicolons, commas and colons. 5. Delete the semicolon after the last declaration of a block and write the result on one line.
This is not a mathematical formula but a tokenising algorithm. The syntax rules are defined in the W3C CSS Syntax Module Level 3. The process removes only unnecessary characters; selectors, values and the order of the rules do not change.
What the inputs mean
- CSS
- The CSS code to minify. It can contain comments, indentation and line breaks. If a curly brace is left unclosed, the process stops and an error is shown.
Worked example: Minifying a button style
- Input: a /* button */ comment, then a .btn selector with the declarations 'color : #fff;', 'content: "a b";' and 'margin: 0 auto;'.
- The /* button */ comment is removed entirely.
- The space before the colon in 'color : #fff' is removed, giving 'color:#fff'; the text "a b" in content keeps its two spaces.
- The last declaration of the block, 'margin: 0 auto;', loses its semicolon and the output becomes one line.
Result: The output is .btn{color:#fff;content:"a b";margin:0 auto}; the string content is unchanged and the unnecessary characters are gone.
How to read the result
Minified CSS is hard to edit, so always keep the source file. Minification does not change the order of the rules or the meaning of selectors, but remember that the last semicolon is gone when you add a new declaration by hand. This is general information.
Common mistakes
- Adding a declaration by hand to minified CSS without a semicolon; the declarations join together and the next rule breaks.
- Expecting the spaces around the > combinator to vanish completely; the tool can keep them.
- Minifying a stylesheet with an unclosed curly brace; the incomplete block can disable the rules that follow.
- Losing licence or source notes that were kept in comments; store that information in a separate file.
- Using minification as a replacement for compression; server-side compression should be applied as well.
Frequently asked questions
Are comments removed completely?
Yes, CSS comments are not included in the output. They have no effect on styling, so removing them is safe.
Do spaces inside strings change?
No. In the example content: "a b", the two spaces are kept, because text inside quotation marks is copied as it is.
Why is the last semicolon removed?
A semicolon after the last declaration of a block has no meaning. Removing it does not change how the browser applies the styles.
How much smaller does the file get?
It depends on the number of comments and the indentation. Files with many comments and deep indentation shrink noticeably.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.