DevToolbox

CSS Formatter

Beautify or minify CSS stylesheets

Input

Paste or type CSS
Loading...

Output

Output will appear here

Related tools

What is CSS formatting?

CSS formatting re-prints a stylesheet with consistent layout: one declaration per line, a space after each colon, a semicolon after every declaration, and nested rules and at-rules indented. This formatter uses Prettier's CSS printer on a real CSS parser, so selectors such as a:hover, strings, url() values, and comments keep their meaning, and CSS it can't parse gets an error with the line and column instead of broken output. Minify uses PostCSS and cssnano. Both run in your browser.

How the formatter lays out CSS

Prettier parses the stylesheet with PostCSS and prints every rule the same way: the selector, then one declaration per line, each with a space after the colon and a semicolon at the end, including the last. Each selector in a comma-separated list gets its own line, and nested rules, @media, @supports, @container, @layer, and @keyframes blocks are indented by 2 or 4 spaces, as you choose.

Lines are kept within 80 characters where the CSS allows it: a long chain of selectors breaks across lines, and a value made of comma-separated parts, such as several box-shadow layers, puts each part on its own line. Comments stay where they were, and blank lines between rules are kept (several in a row become one).

What formatting changes, and what it keeps

Selectors, property values, strings, url() values, comments, and the order of rules and declarations are kept. Prettier does change how some things are spelled, in ways CSS treats as identical: hex colours, property names, and units are lowercased (#FFF becomes #fff, WIDTH: .5PX becomes width: 0.5px), numbers gain a leading zero and lose trailing zeros (.50em becomes 0.5em), and strings and attribute selector values are written in double quotes. Keywords such as Red keep their case, and custom property names keep theirs, because those are case-sensitive.

Errors with line and column

CSS that can't be parsed produces no output. Instead the formatter shows the problem and where it is, such as "Line 1, column 1: Unclosed block." for a rule that is never closed, or "Unexpected }." for an extra brace, and marks it in the editor. Go to error moves the cursor there.

Format for editing, minify for production

Readable CSS is for the people who maintain it; minified CSS is for the browser. Minify uses PostCSS and cssnano's default preset, the same engine as the CSS Minifier: it removes comments (except /*! … */ license comments) and whitespace, writes values more briefly (#ffffff as #fff, 0px as 0), and merges duplicate rules where that can't change which declaration wins. Format while you work, and minify as a build step before deploying.

Formatting and clean diffs

One declaration per line is not only easier to read; it also produces clean diffs in version control, because changing one property touches one line instead of rewriting a whole rule. Consistent formatting is what lets a reviewer see exactly which style changed.

How to format CSS

  1. 1Paste your CSS. Paste a stylesheet or a single rule into the input, or load the sample.
  2. 2Choose Format or Minify. Format re-indents the CSS with 2 or 4 spaces, as you choose; Minify compresses it. If the CSS can't be parsed, the error shows the line and column.
  3. 3Copy the result. Copy the formatted or minified CSS into your project, or share a link to it.

Examples

Format a compressed rule

Input

.btn{color:#FFF;padding:8px 12px;border-radius:6px}

Output

.btn {
  color: #fff;
  padding: 8px 12px;
  border-radius: 6px;
}

Pseudo-classes inside @media

Selectors such as a:hover and ::before are kept exactly.

Input

@media (max-width:600px){a:hover{color:red}.b::before{content:"→"}}

Output

@media (max-width: 600px) {
  a:hover {
    color: red;
  }
  .b::before {
    content: "→";
  }
}

CSS nesting

Input

.card{padding:24px;&:hover{color:blue}.title{font-weight:700}}

Output

.card {
  padding: 24px;
  &:hover {
    color: blue;
  }
  .title {
    font-weight: 700;
  }
}

Comments and url() values

The data URL's colon and semicolon are left as they are.

Input

/* Logo */.logo{background:url(data:image/png;base64,iVBORw0KGgo=) no-repeat}

Output

/* Logo */
.logo {
  background: url(data:image/png;base64,iVBORw0KGgo=) no-repeat;
}

4-space indentation

Input

.a{color:red;b{color:blue}}

Output

.a {
    color: red;
    b {
        color: blue;
    }
}

Spelling that Prettier normalizes

Each change is one CSS treats as identical.

Input

a{WIDTH:.5PX;margin:0.50em;content:'x'}

Output

a {
  width: 0.5px;
  margin: 0.5em;
  content: "x";
}

An unclosed rule

No output is produced; the error is shown instead.

Input

a{color:red

Output

Line 1, column 1: Unclosed block.

Minify for production

Comments and whitespace go, #ffffff becomes #fff, and the duplicate .btn rule is merged.

Input

.btn {
  color: #ffffff;
  padding: 8px;
}
.btn {
  margin: 0px;
}

Output

.btn{color:#fff;padding:8px;margin:0}

What the formatter changes

Layout always changes; these are the other changes, all ones CSS treats as identical.

BeforeAfter
color:#FFFcolor: #fff;
WIDTH:.5PXwidth: 0.5px;
margin:0.50emmargin: 0.5em;
content:'x'content: "x";
a[href^=http]a[href^="http"]
A:HOVERA:hover
color:Redcolor: Red; (keywords keep their case)
--Brand-Color:#F00--Brand-Color: #f00; (custom property names keep their case)

Frequently asked questions

Does formatting change how my styles apply?

No. Selectors, values, strings, url() values, comments, and the order of rules and declarations are kept. Besides the layout, Prettier only changes spellings that CSS treats as identical: lowercase hex colours, property names, and units, a leading zero on numbers such as .5, no trailing zeros, and double quotes around strings.

What happens if my CSS has an error?

The formatter shows what's wrong and where, for example "Line 1, column 1: Unclosed block." or "Unexpected }.", marks it in the editor, and Go to error moves the cursor there. It doesn't produce output from CSS it can't parse, so you never copy broken CSS by accident.

What does minifying CSS do?

Minify uses PostCSS and cssnano's default preset, the same engine as the CSS Minifier. It removes comments (except /*! … */ license comments) and whitespace, writes values more briefly, such as #ffffff as #fff and 0px as 0, and merges duplicate rules where that can't change which declaration wins.

Will it sort or reorder my rules?

Format never reorders rules or declarations, which matters in CSS because later declarations override earlier ones. Minify can merge duplicate rules, but only where the result applies the same styles.

Does it support modern CSS?

Yes. CSS nesting, custom properties, @media, @supports, @container, @layer, @keyframes, and selectors such as :has() and :is() are formatted and indented with the rest of the stylesheet.

Can I choose the indentation?

Yes. Choose 2 or 4 spaces at the top of the tool; nested rules and at-rules are indented by that much per level. Minify ignores it, because minified CSS has no indentation.

Does it handle Sass or Less?

It's built for CSS. Prettier's CSS parser also accepts much SCSS syntax, such as $variables, @mixin, and @include, and keeps it as written, but it isn't a Sass or Less formatter: for .scss and .less files, use Prettier with its scss or less parser.

Which formatter does it use?

Prettier 3's CSS printer, with a line width of 80 characters, running in a background thread in your browser.

Is my CSS sent to a server?

No. Formatting and minifying run in your browser. The only exception is the Share link button, which uploads the current output when you click it.

Is it free?

Yes — free and unlimited.