CSS Formatter

Beautify messy CSS into indented, readable rules or minify it for production. String- and parenthesis-safe, fully offline.

Beautify messy CSS into indented, readable rules — or minify it for production — with a tokenizer that respects strings, comments, and parentheses, so calc(1px + 2px) keeps its spaces and content:"a, b" keeps its comma while selectors still compress correctly.

How to use CSS Formatter

  1. Paste the CSS.
  2. Pick an indent and press Beautify, or press Minify.
  3. Copy the result.

Who it is for

Frontend developers normalising stylesheets from different authors, performance engineers shaving CSS payloads, and anyone who inherited a minified file they need to actually read.

How it works

  • Beautify splits selector lists onto their own lines and indents every declaration; comments survive beautify for a reason.
  • Minify drops comments and collapses whitespace only where CSS semantics allow — string literals and calc parens are untouchable.
  • Both directions run through one tokenizer, so what beautify reads, minify writes back consistently.

What it cannot do

  • It does not rewrite or optimise rules (no autoprefixing, merging, or purging) — formatting only, by design.
  • Vendor-specific hacks survive as written; that is a feature, not an invitation.

Sample input & output

You give

.card,.card-alt{display:flex;gap:calc(1rem + 4px);padding:12px 16px;/* layout */background:#fff}

CSS Formatter returns

.card, .card-alt { display: flex; gap: calc(1rem + 4px); padding: 12px 16px; /* layout */ background: #fff; } (or minified to 79 bytes with comment removed and calc() spaces intact).

Frequently asked questions

Will minify break my calc() values?

No — spaces inside parentheses are preserved because calc(100% - 10px) is invalid without them. Strings get the same protection.

Does minify remove comments?

Yes — comments are development notes, and the minified output is meant for production.

Does it handle nested CSS or preprocessor syntax?

Plain CSS only; run SCSS or Less through their compilers first.

Why must spaces around + and - inside calc() never be stripped?

Under the W3C CSS Values and Units specification, the + and - operators inside calc(), min(), max(), and clamp() require whitespace on both sides to distinguish subtraction from negative numbers or hyphenated custom property identifiers like --gap-size. Stripping spaces turns calc(100% - 20px) into calc(100%-20px), which a compliant CSS parser rejects as a dimension token followed by a signed length, invalidating the entire declaration.

How does CSS formatting affect CSS custom properties (--variables) with empty or whitespace values?

Custom properties follow permissive tokenization rules under the CSS Custom Properties for Cascading Variables specification, where --empty: ; (a single space) is a valid guaranteed-invalid toggle token used in cyclic space-toggle tricks. A naive regex minifier collapses that space to --empty:; which changes behavior in older engines, whereas token-aware formatting preserves quoted strings, url() arguments, and declaration values without corrupting variable fallbacks.

Does beautifying or minifying CSS alter cascade specificity or rule evaluation order?

No. Cascade resolution depends strictly on origin, @layer order, selector specificity weights (ID, class/attribute/pseudo-class, and type/pseudo-element columns), and source order. Splitting a comma-separated selector list like .btn, .link onto separate lines or collapsing whitespace between declarations leaves the CSS Object Model (CSSOM) identical, so computed styles, inheritance, and media query breakpoints evaluate identically before and after formatting.

Why do @import and @charset rules fail if comments or rules move above them?

In CSS Syntax Module Level 3, @charset must begin at byte zero of the stylesheet with exact double-quoted syntax, and any @import rules must precede all style rules other than @charset and empty @layer statements. Beautifying keeps top-level at-rules in their exact source sequence so browser preload scanners still honor external imports, while minifying strips leading block comments that could otherwise displace byte-zero declarations.