CSS Formatter

Paste minified or badly indented CSS and get it back readable. The same box minifies in the other direction when you are done. Format keeps your comments; Minify removes them.

Input CSS
Formatted / Minified CSS

1 CSS Formatter example

Input (Minified)

1
body{font-family:sans-serif;background:#fff}h1{color:#333;margin-bottom:20px}.card{padding:20px;border:1px solid #ddd}

Output (Formatted)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
body {
  font-family: sans-serif;
  background: #fff
}

h1 {
  color: #333;
  margin-bottom: 20px
}

.card {
  padding: 20px;
  border: 1px solid #ddd
}

2 What the CSS Formatter helps with

Reading someone else's stylesheet

Minified CSS is one long line. Indented, each rule and declaration sits on its own line, so you can find the rule that overrides yours.

Shipping it smaller

Minify strips comments and whitespace, which is most of the weight in a hand-written stylesheet. It is a text pass, not a parser, so it also removes spaces around colons and commas inside quoted strings. For stylesheets with content strings, use the CSS minifier, which protects them.

How to use the CSS Formatter tool

  1. Paste CSS into the left editor, or use Upload File for a .css file. Load URL fetches a stylesheet if the host allows cross-origin requests.
  2. Pick 2 Spaces, 4 Spaces or Tab from the indentation dropdown.
  3. Click Format. Each selector and declaration goes on its own line, and nested @media and @supports blocks are indented.
  4. Click Minify to remove comments, line breaks and unneeded whitespace. It is a quick text pass that does not protect quoted strings, so use the CSS minifier for production files.
  5. Click Copy Result to copy the output. The header of each panel shows its length in characters, so you can see how much Minify saved.

How to beautify and minify CSS in JavaScript, Python and PHP

JavaScript / Node.js

const css = `
/* buttons */
.btn {
  color: #fff;
  background: blue;
}
@media (max-width: 600px) { .btn { display: block; } }
`;

// Minify: strip comments, collapse whitespace, drop the last semicolon in each block
const minified = css
  .replace(/\/\*[\s\S]*?\*\//g, "")
  .replace(/\s+/g, " ")
  .replace(/\s*([{}:;,>])\s*/g, "$1")
  .replace(/;}/g, "}")
  .trim();
console.log(minified);
// .btn{color:#fff;background:blue}@media (max-width:600px){.btn{display:block}}

// Beautify with js-beautify (npm i js-beautify):
// const { css: beautify } = require("js-beautify");
// console.log(beautify(minified, { indent_size: 2 }));

The regex minifier handles simple stylesheets and will eventually mangle a complex one. For production, use cssnano, lightningcss or esbuild, which parse strings and url() values properly.

Python (cssbeautifier)

# pip install cssbeautifier
import cssbeautifier

css = ".btn{color:#fff;background:blue}@media (max-width:600px){.btn{display:block}}"

opts = cssbeautifier.default_options()
opts.indent_size = 2
print(cssbeautifier.beautify(css, opts))

# From the shell:  css-beautify --indent-size 2 style.min.css

cssbeautifier is the Python port of js-beautify, so its output matches this tool.

PHP

<?php
$css = '.btn{color:#fff;background:blue}@media (max-width:600px){.btn{display:block}}';

// Minimal beautifier: one declaration per line, 2-space indent
function formatCss(string $css, string $indent = '  '): string {
    $out = '';
    $depth = 0;
    for ($i = 0, $len = strlen($css); $i < $len; $i++) {
        $c = $css[$i];
        if ($c === '{') {
            $out = rtrim($out) . " {\n" . str_repeat($indent, ++$depth);
        } elseif ($c === '}') {
            $out = rtrim($out) . "\n" . str_repeat($indent, --$depth) . "}\n" . str_repeat($indent, $depth);
        } elseif ($c === ';') {
            $out .= ";\n" . str_repeat($indent, $depth);
        } elseif ($c === ':' && $depth > 0) {
            $out .= ': ';
        } else {
            $out .= $c;
        }
    }
    return trim($out) . "\n";
}
echo formatCss($css);

// Minify
echo preg_replace(['~/\*.*?\*/~s', '/\s*([{}:;,])\s*/', '/;}/'], ['', '$1', '}'], $css);

This ignores edge cases such as colons inside url() or content strings. For a full implementation use sabberworm/php-css-parser.

Common errors and how to fix them

Formatted output has a stray } or the nesting is wrong
An unbalanced brace or an unclosed comment earlier in the file. Formatters do not validate, so search for /* without a matching */ and check that every rule closes.
url(data:image/svg+xml;...) is broken after minifying
A naive minifier treats the ; or , inside the data URI as a separator. Wrap the URL in quotes, or use a CSS minifier that parses url() tokens.
content: "\201C" became a different character
Some tools resolve Unicode escapes in strings. Keep the escaped form in source, save the output file as UTF-8, and add @charset "UTF-8"; as the first line if needed.
Load URL returns nothing for a CDN stylesheet
Most CDNs allow cross-origin requests, but some do not, and then the browser blocks the response. Download the file and use Upload File instead.

Good to know

Reformatting CSS cannot change how it behaves. The cascade depends on source order and specificity, not whitespace, and a formatter keeps rule order and never shuffles declarations. The beautified file and the minified file are the same stylesheet.

Dedicated minifiers do more than strip whitespace. cssnano and lightningcss shorten colors (#ffffff to #fff), merge duplicate selectors and drop the last semicolon in each block. The Minify button here removes comments and whitespace and drops that last semicolon, and nothing more.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Does it support minification?

Yes. Use Minify to strip comments and whitespace, or Format to expand a minified file for editing. Minify here is a quick text pass that does not protect quoted strings; for production files, the CSS minifier is the safer choice.

Are media queries and nested rules handled?

Yes. Rules inside @media, @supports and @keyframes are indented one level deeper, and native CSS nesting is preserved.

Will it reorder my properties?

No. Property order and selector order stay exactly as written; only spacing changes.

Can I choose the indentation?

Yes. The dropdown offers 2 Spaces, 4 Spaces or Tab.

Does it work with SCSS or LESS?

Plain CSS is the target. For SCSS, use the SCSS compiler, which outputs formatted CSS.

Related

SCSS to CSS if the source is Sass, and Purge CSS when the file is large and you suspect most of it is unused. For markup, there is the HTML formatter.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!