About CSS Minifier: Guide & Practical Notes
Naive regex CSS minifiers destroy stylesheets in seconds—stripping essential spaces inside `calc(100% - 2rem)` or corrupting semicolons inside `content: ";"` strings.
Minifies CSS safely using a state-machine tokenizer that removes comments, redundant whitespace, and trailing semicolons before `}` while preserving quoted strings, custom properties, and `calc()` arithmetic spacing.
How to Use CSS Minifier Step by Step
- Paste CSS rules into Input Code.
- Click Minify Code to view compressed CSS and exact byte savings.
- Copy the minified stylesheet.
Worked Example
Compress a card stylesheet containing a `calc()` expression and comment.
Inputs: .card { width: calc(100% - 2rem); /* border */ border: 1px solid #e2e8f0; }
Result: .card{width:calc(100% - 2rem);border:1px solid #e2e8f0}
Comments and trailing `;` disappear while `100% - 2rem` keeps required spaces around `-` so `calc()` stays valid!
Tokenizer-Safe vs. Naive Regex CSS Minification
In CSS Values & Units, `+` and `-` inside `calc()` MUST be surrounded by whitespace to avoid parsing as signed dimension tokens (`100%-2rem` is invalid!). Character-by-character quote and comment scanning prevents regex bugs.
Common Real-World Uses
- Inlining compact critical CSS inside `<style>` heads for faster LCP
- Trimming third-party snippet styles before embedding
- Measuring byte savings on custom CSS modules
Common Mistakes to Avoid
- Running simple `.replace(/\s+/g, "")` regexes that break `margin: 10px 20px` and `calc()`.
- Stripping spaces inside `data:image/svg+xml` URLs accidentally.
Supported Formats, Scope & Limitations
Performs lossless syntactic compression rather than merging duplicate selectors or rewriting vendor rules.
Privacy & Local Processing
Stylesheets remain solely inside your browser.
Frequently Asked Questions
Does this keep spaces inside `calc(100% - 20px)` intact?
Yes—our tokenizer explicitly protects operator spacing around `+` and `-` as well as quoted string literals.
Can I reverse minified CSS back to readable lines later?
Yes—paste minified CSS into our CSS Formatter anytime.
Need to unminify a compressed stylesheet for debugging? Open the CSS Formatter.