CSS Formatter
Format, beautify, and minify CSS code online. Configurable indentation, comment handling, and instant validation in a single client-side tool.
About
Malformed CSS causes silent rendering failures across browsers. A missing brace shifts every subsequent rule. A misplaced semicolon inside a calc() expression breaks the entire declaration block. This tool parses your stylesheet character by character, reconstructs the rule tree, and re-emits it with consistent indentation, brace placement, and whitespace. It handles nested @media and @supports blocks, preserves or strips comments, and flags unclosed braces before they reach production. Minification mode strips all non-essential bytes, reducing payload size by 20 - 40% on typical stylesheets.
The formatter approximates standard Prettier/stylelint output conventions. It does not reorder properties or perform semantic analysis. Shorthand collapsing and vendor-prefix deduplication are outside scope. For critical stylesheets, always validate against a browser rendering test after formatting.
Formulas
CSS formatting is a structural transformation, not a mathematical operation. The core logic follows a finite-state tokenizer model:
For each character c at position i, the tokenizer transitions between states based on a deterministic rule set:
Minification output size Smin relative to original Sorig:
where W = total whitespace bytes and C = total comment bytes (when stripping is enabled).
Reference Data
| Feature | Beautify Mode | Minify Mode |
|---|---|---|
| Indentation | Configurable (2/4 spaces or tab) | None |
| Newlines after { | Yes | No |
| Newlines after ; | Yes | No |
| Trailing semicolons | Preserved/Added | Removed (last in block) |
| Comments | Preserved (configurable) | Stripped (configurable) |
| Empty lines between rules | Yes (1 blank line) | No |
| Selector-per-line | Each selector on own line | All on one line |
| Whitespace around : | Space after colon | No space |
| Whitespace around { | Space before brace | No space |
| Nested @-rules | Indented per depth level | Flattened |
| Typical size reduction | - | 20 - 40% |
| Parse error detection | Yes (warnings shown) | Yes (warnings shown) |
| String literal safety | Preserved verbatim | Preserved verbatim |
| Unicode escape handling | Preserved | Preserved |
| Max tested input | 500KB | 500KB |