CSS Minifier

Minified CSS—
Before (chars)—
After (chars)—
Saved (%)—

How CSS Minifier is calculated: Strip comments outside strings, collapse whitespace, drop spaces around {} : ; , and remove the last semicolon before } — strings are never touched.

What is a CSS minifier?

A CSS minifier takes a stylesheet and strips out every character the browser does not need: comments, spaces, newlines, and indentation. The result is the same stylesheet on fewer lines, doing exactly the same job. Nothing about the styling changes — the selectors, the properties, and their values all stay exactly the same.

The default text on this page shows the idea clearly. It starts as 49 characters over five lines, with a comment and two-space indents. Minified, it becomes 24 characters on one line: body{color:red;margin:0}. That is 25 characters gone, a saving of 51.0% — and the rule styles the page identically.

The need comes up whenever a site is shipped. Browsers download CSS before they paint the page, and every byte costs time — especially on slow connections. Minified stylesheets are smaller, faster, and cheaper to serve, which is why every production site minifies. This tool does the same job for a pasted block.

This minifier is careful, not aggressive. It knows which spaces are decoration and which are load-bearing, and it never touches the characters inside a string. A minifier that strips spaces blindly breaks stylesheets; this one strips only what is safe to strip, and says plainly what it leaves alone.

How CSS minifying works

Minifying has three passes, and each one is string-aware. The first pass strips comments: everything between /* and */ is removed, unless it sits inside a quoted string. A comment-looking piece of text inside a content value — like "/* not a comment */" — is data, not a comment, and it survives untouched.

The second pass collapses whitespace. Runs of spaces, tabs, and newlines become a single space, and then that space is dropped whenever it sits next to punctuation that does not need it: before and after braces, colons, semicolons, and commas, and after an opening bracket or before a closing one. What is left is the smallest spacing that keeps the CSS valid.

The third pass removes the last semicolon before a closing brace. In body{color:red;margin:0;} the semicolon before } does nothing — the brace already ends the declaration — so it is dropped, giving body{color:red;margin:0}. One character per rule, which adds up across a whole stylesheet.

The size numbers tell the story. Before is the character count of what you pasted, after is the character count of the minified result, and saved is the percentage difference. On the default block the saving is 51.0%; on real stylesheets it is usually 20–30%, because real CSS has less comment overhead and more values that cannot shrink.

What the minifier never touches

Strings are sacred. Anything inside single or double quotes is copied character for character — spaces, comment-looking text, escaped quotes, all of it. The test for it is simple: .a{content:"/* not a comment */";color:#fff} minifies to itself, 44 characters in and 44 characters out, with 0% saved. The string was the whole point, and it survived.

The spaces inside calc() are sacred too. The expression calc(100% - 20px) needs its spaces around the minus sign — without them, the browser cannot parse it. The minifier keeps those spaces, turning .w{width: calc(100% - 20px);} into .w{width:calc(100% - 20px)} — 29 characters down to 27, a saving of 6.9%, with the maths intact.

Descendant combinators are sacred as well. The space in .a .b means 'b inside a', and removing it would change the selector to .a.b, which means something else entirely. The minifier keeps that space, turning a three-line rule into .a .b{color:red} — 23 characters down to 16, a saving of 30.4%, with the selector meaning unchanged.

Media queries keep their space too. @media (max-width: 600px) stays written with a space after @media, because that is the readable and safe form. The example in this article goes from 59 characters to 42 — @media (max-width:600px){.box{width:100%}} — a saving of 28.8%, with the query still working exactly as before.

A worked example: a comment, a string, and a query

Start with the default block: a comment, then a body rule with two declarations, spread over five lines and 49 characters. Minified, it is body{color:red;margin:0} — 24 characters, 51.0% saved. The comment is gone, the indents are gone, the last semicolon is gone, and the page looks exactly the same.

Now the string test: .a{content:"/* not a comment */";color:#fff}. It starts at 44 characters and stays at 44, with 0% saved. That is the correct answer — the text inside the quotes is data, and the minifier refused to touch it. A tool that stripped it would have broken the page.

Try a media query: @media (max-width: 600px) with a .box rule inside, spread over five lines and 59 characters. Minified, it becomes @media (max-width:600px){.box{width:100%}} — 42 characters, 28.8% saved. The space after @media stayed, the spaces around the colon went, and the query still matches screens up to 600 pixels wide.

Finally, the calc test: .w{width: calc(100% - 20px);}. Minified, it is .w{width:calc(100% - 20px)} — 29 characters down to 27, 6.9% saved. Only the space after the colon and the last semicolon went; the spaces around the minus sign stayed, because the maths needs them.

Who minifies CSS, and why

Front-end developers are the biggest users. Stylesheets are written pretty so humans can edit them, then minified before they are shipped, because browsers download CSS before they paint the page. Minifying by hand is a waste of time and a source of mistakes; pasting the block here takes two seconds.

Site owners are the second group. A faster page keeps visitors, and CSS is part of every page load. Cutting a quarter of the stylesheet's size will not fix a slow site on its own, but it is one of the cheapest wins available — free, instant, and impossible to get wrong when a parser does it.

Template and email developers are a third group. They work with CSS in small pieces — a snippet for a page, a style block for an email — and they need those pieces small without breaking them. A careful minifier shrinks the snippet while keeping the strings and the calc() expressions intact.

Students are a fourth group. Minifying is the fastest way to see what CSS actually is once the layout is stripped away: selectors, braces, declarations, semicolons — nothing else. It teaches the difference between the styling and the decoration, which is the first thing anyone working with CSS needs to learn.

CSS minifier vs other text tools

A CSS minifier is a specialist, and the general tools around it do different jobs. A text formatter cleans up plain text, and a remove extra spaces pass collapses whitespace in any text — but neither one knows that calc() needs its spaces or that a string is sacred. CSS needs a tool that knows CSS.

The JSON tools in the same family follow the same pattern. A JSON formatter prints JSON neatly, and this minifier squeezes CSS tight — both are format-aware, and both refuse to corrupt the data they handle. Format-aware beats text-aware every time, for any format.

Counting the size of the result is a separate job. A word counter tells you how big a block is, and an HTML encoder decoder handles the escaped characters that HTML and CSS share in style blocks. Minify first, then measure — in that order.

One honest limit is worth stating: this minifier does not shorten anything. It does not turn #ffffff into #fff, it does not drop units from zero values, and it does not merge duplicate rules. Those are optimisations, and they belong to a full build tool. This tool removes the air — comments and whitespace — and leaves the values exactly as you wrote them.

Common minifying mistakes

The most common mistake is minifying with a text search-and-replace instead of a string-aware tool. Removing all spaces from CSS by hand also removes the spaces inside strings and inside calc(), which breaks content values and maths. Always minify with a tool that tracks whether it is inside a string.

Second: minifying and then editing the result by hand. Minified CSS is meant to be read by browsers, not people. If you need to change a value, go back to the pretty version, edit that, and minify again. Editing a single-line stylesheet by hand is how braces get lost and rules get mismatched.

Third: assuming minified CSS is compressed. It is not. Minifying removes comments and whitespace, which is usually a fifth to a third of the size, but real compression — the kind a web server does — shrinks the rest. Minifying and compression work together: minify first, then let the server compress what is left.

Fourth: minifying CSS that is already broken and expecting the tool to fix it. A minifier cannot repair a missing brace or a bad declaration. If the result looks wrong, check the input first — the minifier only removes what is safe to remove, and a broken rule stays broken, just shorter.

How to use this CSS minifier

Using this CSS minifier is simple. Paste your CSS into the box, or type it directly. The minified result appears instantly in the box below, with the before and after character counts and the percentage saved shown above it. There is no button to press and no form to submit.

To use the result, select the text in the output box and copy it. To minify another block, select all, delete, and paste the new one — the counts reset instantly. If the CSS contains a string with a comment inside it, the string survives untouched, exactly as it should.

Everything runs in your browser. No sign-up, no account, no data sent anywhere — your CSS never leaves your device. It works offline once the page has loaded, so you can use it on a plane, in a server room with no signal, or anywhere else you work with stylesheets.

Try it with something real before you need it: paste a rule from a stylesheet you work on and watch a quarter of its size disappear. Once you have used it twice, you will never ship pretty CSS to a server again — and you will never minify by hand either.

Frequently Asked Questions

No. Minifying only removes comments and whitespace that the browser does not need, plus the last semicolon before each closing brace. The selectors, properties, and values stay exactly the same, so the page renders identically. The default block goes from 49 characters to 24 — body{color:red;margin:0} — with no visible change at all.

Never. The expression calc(100% - 20px) needs its spaces around the minus sign — without them, the browser cannot parse it. This minifier keeps those spaces, turning .w{width: calc(100% - 20px);} into .w{width:calc(100% - 20px)}. Only the space after the colon and the last semicolon are removed.

It is copied character for character, including spaces and comment-looking text. The rule .a{content:"/* not a comment */";color:#fff} minifies to itself — 44 characters in, 44 out, 0% saved — because the string is data, not decoration. A minifier that stripped it would break the page.

No. It does not turn #ffffff into #fff, it does not drop units from zero values, and it does not merge duplicate rules. Those are optimisations that belong to a full build tool. This tool removes the air — comments and whitespace — and leaves every value exactly as you wrote it.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.