How do I format minified CSS to make it readable?

Dark navy cover card with the title 'How do I format minified CSS to make it readable?' in white and a green accent bar

To format minified CSS, run it through a formatter: your browser's DevTools, an editor command, Prettier, or an online CSS formatter. Each one puts every rule and declaration back on its own line with indentation, so the stylesheet is readable again. What none of them can do is bring back comments or the original source structure, because minification deleted those.

Which route is fastest depends on where the CSS is right now: on a live site, in a file on disk, or in your clipboard.

What does formatting minified CSS actually do?

Formatting minified CSS adds back the whitespace a minifier removed: a line break after each {, ; and }, plus indentation inside each block. The rules themselves stay the same, so the browser renders the page exactly as before.

Minification strips code comments and white space, and can also drop unused code. A formatter reverses only the whitespace part. Here is a small example. The minified input:

.card{margin:0 auto;padding:1rem}.card>h2{color:navy}@media print{.card{padding:2rem}}

And the same CSS after formatting:

.card {
  margin: 0 auto;
  padding: 1rem;
}

.card > h2 {
  color: navy;
}

@media print {
  .card {
    padding: 2rem;
  }
}

The media query's nesting is visible now, and the declarations scan one per line.

How do you format minified CSS from a live website?

For CSS on a live site, browser DevTools already does the formatting for you. You don't need to copy anything out.

Chrome and Edge. Open DevTools, go to the Sources panel and click the stylesheet. Chrome's own reference states that the Sources panel pretty-prints minified files by default. A button in the bottom left corner of the editor switches back to the file as it was loaded.

Firefox. Open the Style Editor tab. It automatically de-minimizes style sheets that it detects, without changing the original, and each sheet has a Save button to keep a copy.

Both tools also read CSS source maps. If the site ships one, you may see the original Sass or Less files instead of a formatted bundle.

How do you format a minified CSS file on your computer?

For a file on disk, use Prettier, css-beautify, or the formatter built into your editor. They differ in defaults and in how much setup they need.

Prettier

Prettier picks the CSS parser from the file extension, so no configuration is required:

npx prettier --write styles.min.css

The --write flag formats the file in place, and the docs warn that it overwrites your files. To print the result instead, pipe the file in:

cat styles.min.css | npx prettier --stdin-filepath styles.css > styles.readable.css

Prettier's defaults are a tab width of 2 and a print width of 80. It has no CSS-only options, which is the point: you get one consistent style.

css-beautify

The js-beautify package installs a css-beautify command (the same as js-beautify --css):

npm -g install js-beautify
css-beautify styles.min.css > styles.readable.css

Output goes to the terminal unless you redirect it. The default indent is 4 spaces. Add -N for a blank line between rules and -L to put each selector in a comma-separated list on its own line.

VS Code

VS Code has a CSS formatter built in, based on the JS Beautify library, and it also handles SCSS and Less. Open the file and run Format Document from the command palette. Settings such as css.format.newlineBetweenRules and css.format.spaceAroundSelectorSeparator control blank lines between rulesets and spaces around >, + and ~.

A large minified file is a single very long line, and editors can be slow with that. If VS Code struggles, format from the command line first and open the result.

When is an online CSS formatter the better choice?

An online CSS formatter is the quickest option when the CSS is in your clipboard and you have no project open. A snippet from a support ticket, say, or an email template or a CMS field.

Paste the minified CSS into the CSS formatter and copy or download the result. On OnlineViewer Dev the input is processed by JavaScript in the page, and the tool code makes no network requests with it. That matters if the stylesheet belongs to a client or an unreleased product.

My take: for a one-off read, use DevTools or a browser formatter and move on. Prettier is only worth installing when the formatted file will live in a repository, where the consistent style pays for the setup. The Prettier versus online formatter comparison covers that trade-off.

Where the CSS is Fastest way to read it
On a live site DevTools Sources panel or Firefox Style Editor
In a file in a project Prettier, or Format Document in VS Code
In a file, no project css-beautify or an online formatter
In your clipboard Online formatter

What can't a formatter bring back?

A formatter can't restore anything the minifier deleted or rewrote. Formatted CSS is readable, but it is not the original source. This is the part most tool pages skip.

Expect these gaps:

  • Comments are gone. Section headers, licence notes and "don't touch this" warnings were removed during the build.
  • Rewritten values stay rewritten. If the minifier shortened a colour or merged several declarations into a shorthand, you get the short form back, just on its own line.
  • Preprocessor structure is flattened. Sass variables, mixins and nesting were compiled away before minification. You see plain CSS.

File boundaries go too: a bundle built from many partials comes back as one long file. And a class name generated by a build tool stays hashed, because it has no readable original inside the stylesheet.

A formatted copy is for reading and debugging, not for maintaining. Edit it and ship it, and you have two diverging versions of the same styles. If the site is yours, edit the source and minify the CSS again in the build.

How do you check for a CSS source map first?

Look at the last line of the minified file. A source map is a JSON file that maps the minified code back to its original, unmodified form, and a stylesheet points to one with a comment like this:

/*# sourceMappingURL=index.css.map */

The browser can also learn about the map from a SourceMap HTTP header. When either is present, DevTools can show the original files, comments and all. In Firefox, original sources are displayed by default in the Style Editor.

Many production sites don't publish their maps. When the comment is missing, a formatted copy is the best you can get from the outside. The same limit applies to scripts, which is why minified JavaScript is harder to read than minified CSS even after formatting: there, variable names get shortened too.

What goes wrong when formatting minified CSS?

Formatting itself is safe, but three things trip people up.

  1. Overwriting the only copy. prettier --write replaces the file. If styles.min.css is the build output a page loads, you have just made it larger. Write to a new file name instead.
  2. Invalid CSS in, odd output out. A missing brace in the input makes a formatter indent everything after it one level too deep. That's useful: the point where indentation goes wrong is usually where the error is.
  3. Assuming whitespace never matters. Spaces between rules don't matter. Spaces inside values can, for example around the operators in calc(). A real formatter leaves those alone, but a hand-rolled find-and-replace may not.

Once the file is readable, common next steps are to pull the colours out of the CSS to see the palette, or to convert px values to rem while you tidy up.

FAQ

Can I unminify CSS back to the original file?

Only the layout. A formatter puts back line breaks and indentation, but comments and anything the minifier merged or shortened are not in the file any more. To see the real original you need the source map or the source repository.

Does formatting CSS change how the page looks?

No. Formatting only adds whitespace between rules and declarations, which the browser ignores. Spaces inside values, such as in calc() or around selector combinators, are kept by a proper formatter.

How do I pretty print CSS in Chrome?

Open DevTools, go to the Sources panel and click the stylesheet. Chrome pretty-prints minified files by default. A button in the bottom left corner of the editor switches back to the file as it was loaded.

Can Prettier format a minified CSS file?

Yes. Run npx prettier --write on the file to rewrite it in place, or pipe it through prettier --stdin-filepath to print the formatted CSS without touching the file.

Does VS Code format CSS without an extension?

Yes. VS Code ships a CSS formatter built on the JS Beautify library. Run Format Document on the open file, and tune it with the css.format settings.

Sources