How to format and minify CSS in the browser
Every stylesheet lives two lives. In one, a human opens it, scrolls to a selector and changes a value. In the other, a browser downloads it and does not care how it looks. Formatting and minifying are the two directions of that same trip, and getting them right saves both review time and bandwidth.
Why formatted CSS still matters
A minified file gives you one very long line. Search works, but reading does not. When a layout bug lands in production and you have to trace which rule wins, a file with one declaration per line and consistent indentation turns a ten minute hunt into a two minute one.
The same applies to code review. A diff against a single 4,000 character line tells you nothing. A diff against a properly broken up file shows exactly which property changed.
What the formatter actually changes
Formatting is whitespace work. It collapses runs of spaces, puts a newline where a rule ends and indents declarations inside a block. It never renames selectors, reorders declarations or rewrites values, so the output is safe to compare against the input.
- Horizontal layout — one rule per line, declarations kept inline:
body{margin:0;font-size:12px;}. Compact, good for scanning a long file. - Vertical layout — one declaration per line, indented inside the braces. Better when you are editing values by hand.
- Comment stripping — removes
/* ... */blocks. Useful before minifying; keep them on while you are still working.
What minifying removes
Minification goes the other way: every line break, every indent and every redundant space that the CSS grammar allows is dropped. A 60 KB stylesheet commonly lands between 45 and 50 KB before gzip. The result is functionally identical and much harder to read, which is why teams keep the readable version in source control and ship the minified one.
A workflow that holds up
- Keep the readable file in the repository. Editors and code review both depend on it.
- Before a release, run the minifier and store the output as a build artefact rather than editing it by hand.
- Check the byte size before and after, so a stray copy of the original file does not end up in the bundle.
- If something breaks only in production, reformat the shipped file and diff it against the source to see what the build step changed.
Common mistakes
- Minifying already minified CSS twice. Harmless but pointless, and it makes the diff noisy.
- Stripping comments that are load-bearing. Some build tools read marker comments such as
/*! keep */; check before you drop them. - Copying the result out of the tool and losing the original. Keep both, or you will be formatting a 4,000 character line by hand later.
- Assuming the formatter fixed a syntax error. It reformats tokens, it does not parse and repair broken rules — an unbalanced brace stays unbalanced.
Try it without uploading anything
The CSS formatter on this site runs entirely in your browser. Paste a stylesheet, pick a layout mode, format or minify, and copy the result back into your editor. No request leaves the page, which matters when the CSS you are debugging belongs to a client.
Previous: None