CSS Minifier
Minify CSS with a scanner that protects strings, data URIs, calc() spacing and attribute selectors, and keeps licence comments.
CSS Minifier workspace
Source Editor
Press Ctrl/Cmd + Enter to run.
Your draft is kept in this tab only (sessionStorage) and disappears when the tab closes. .
Result Console
Using CSS Minifier
Paste your stylesheet into the editor, or press Load example to see the tricky cases handled.
Press Minify CSS to run the context-aware scanner over it.
Read the panel for the original size, the minified size and the percentage saved.
Copy or download the result, then serve it with gzip or Brotli for the larger saving.
Minifying CSS safely means knowing which context each character sits in, because a comma inside a quoted value and a comma between selectors need opposite treatment. The scanner tracks strings, comments, unquoted url() payloads and parenthesis depth, so string contents, data URIs and attribute-selector values come through exactly as written.
Paste a stylesheet, press Minify CSS, and get back the same rules with the whitespace and comments removed. The panel reports the original size, the minified size and the percentage saved, all measured as UTF-8 bytes rather than characters.
Why whitespace removal needs a scanner
Deleting spaces from CSS sounds like a job for three find-and-replace passes. It is not, because CSS has four contexts in which a space or a comma means something, and a text-level pass cannot tell them apart. The minifier here walks the stylesheet character by character and tracks which context it is in: inside a quoted string, inside a comment, inside an unquoted url(), or at some depth of parentheses.
Here is what the previous, simpler version did to a perfectly ordinary rule:
.card::after { content: "hello, world"; } -> content:"hello,world"
.badge::before { content: "/* x */"; } -> the declaration vanished
a[title="a, b"]:hover { opacity: .8; } -> selector no longer matchedThe first lost a space that was rendered on the page. The second saw a comment where there was a string. The third rewrote an attribute selector's value, which means the rule silently stopped applying to the element it was written for. None of the three produced an error; they produced a stylesheet that was wrong.
What the current pass changes
- Comments are removed, except
/*! ... */, the conventional marker for a licence header that has to survive minification. - Runs of whitespace collapse to a single space, and that space is dropped entirely where it cannot matter — around
{,},;,:,,and the combinators. - Descendant combinators keep their space, because
.a .band.a.bare different selectors. The child, adjacent-sibling and general-sibling combinators lose theirs, so.card > .titlebecomes.card>.title. - The final
;before a closing brace goes, as do repeated semicolons.
Inside parentheses the rules tighten: spaces are removed around commas and after an opening paren, but the spacing inside calc() is left alone. calc(100% - 2rem) is not the same expression as calc(100%-2rem) — the CSS specification requires whitespace around the + and - operators there, and stripping it produces a declaration the browser drops.
Quoted string contents pass through untouched, escapes included. An unquoted url() payload is emitted verbatim, which is what keeps a long base64 data URI intact.
What it will not do
No property reordering, no merging of adjacent rules with identical selectors, no shorthand collapsing, no removal of declarations later rules override. It also does not shorten colours: #223344 stays as written rather than becoming #234. Vendor prefixes are left exactly as you typed them, neither added nor removed on any assumption about your browser targets.
Those omissions are the honest kind. Structural CSS optimisation needs a full parser with a cascade model, which is what cssnano and Lightning CSS are for, in a build pipeline where the output gets tested. This page is for the common case: a stylesheet you want smaller before it goes somewhere, with nothing rearranged behind your back.
What kind of saving to expect
A hand-written, well-commented stylesheet typically drops 15-30%. One that came out of a preprocessor and is already tightly written may only drop a few per cent. Either way, gzip or Brotli on the server will do far more for transfer size than minification does, and the two compose — minify first, compress second.
If the markup that consumes these rules also needs tidying, HTML Formatter reindents it from the parsed DOM. For converting a colour between hex, RGB and HSL notations before you paste it in, use Color Converter.
Your stylesheet is processed in the page you are reading; nothing is posted to a server, and the editor contents live in sessionStorage for this tab alone.
Frequently asked
Does it shorten hex colours or reorder properties?
Neither. A six-digit hex value stays as written, declarations keep their order, vendor prefixes are untouched and no rules are merged. An older version of this page advertised colour shortening that the code never contained. Structural work of that kind needs a full parser with a cascade model, such as cssnano or Lightning CSS.
Why is the space inside calc() left in place?
Because the specification requires whitespace around the plus and minus operators there. Turning calc(100% - 2rem) into calc(100%-2rem) produces a declaration the browser simply drops, so the scanner tracks parenthesis depth and leaves that spacing alone.
Are comments always removed?
All of them except those opening with an exclamation mark, the conventional marker for a licence header that must survive minification. Those are copied through verbatim.
How much smaller should I expect the file to get?
A hand-written, well-commented stylesheet usually drops 15 to 30 per cent. One generated by a preprocessor may barely move. Gzip or Brotli on the server does far more for transfer size, and the two combine well: minify first, compress second.