Developer Tools

CSS Formatter

CSS formatter that beautifies messy stylesheets with 2-space, 4-space or tab indents, or minifies CSS to one line and shows the bytes saved.

Input CSS
Output

    

This CSS formatter puts each rule and each declaration of a stylesheet on its own line, indented with 2 spaces, 4 spaces or a tab, with nested blocks such as media queries one level deeper. Minify does the reverse for production: it collapses whitespace, removes the spaces around braces, colons, semicolons and commas, and drops the last semicolon in each block. Both buttons strip comments and run in your browser, and a size panel compares input and output in bytes.

How to use the CSS formatter

  1. Paste your CSS into the Input CSS box, or press Try Example to load a sample stylesheet.
  2. Choose the indent and click Beautify, or press Ctrl+Enter (Cmd+Enter on a Mac) in the input box.
  3. Review the output. Comments are gone, so keep a copy of the original if you need them.
  4. Use the Minify button to put the CSS on one line for production and see how many bytes it saved.
  5. Copy the result with the Copy button above the output. Clear empties both panels.

What Beautify and Minify change

ChangeBeautifyMinify
CommentsRemovedRemoved
DeclarationsOne per line, indentedAll on one line
Space before {Exactly oneNone
Space around colonsKept as you wrote itRemoved
Selector listsKept on one line, e.g. .btn, .linkSpace after commas removed
Last semicolon in a blockKept if present, never addedRemoved
Values and colorsUnchangedUnchanged

The CSS formatter fixes layout only. It does not add a missing semicolon, sort properties or tell you about syntax errors, so margin : 0 auto ; comes out with its spacing intact on a new line. A blank line separates top-level blocks.

Example: beautify CSS from one line

Input:

.card{padding:1.5rem;border-radius:8px}.card h2{margin:0 0 .75rem;font-size:1.25rem}@media (max-width:768px){.card{padding:1rem}}

Output with 2 spaces:

.card {
  padding:1.5rem;
  border-radius:8px
}

.card h2 {
  margin:0 0 .75rem;
  font-size:1.25rem
}

@media (max-width:768px) {
  .card {
    padding:1rem
  }
}

Going the other way, a 131-byte block with a comment, 4-space indents and two rules minifies to .btn{padding:0.5rem 1rem;border-radius:4px}.btn:hover{background:#1f5c99;color:#fff}, which is 84 bytes. The size panel reports that as 47 B smaller (35.9%).

When to minify CSS and when to format CSS

Format CSS when you need to read it: a minified file from a theme, a stylesheet copied from browser dev tools, or code someone wrote all on one line. Minify CSS before you ship a small stylesheet that has no build step. Projects with a bundler usually minify during the build, so run this tool on the source, not on the output.

Code editors can do the same job as this CSS formatter on files you have open. VS Code's Format Document command (Shift+Alt+F on Windows) formats CSS without an extension, and Prettier does it from the command line with npx prettier --write styles.css. This page is quicker for a snippet you do not want to save as a file, or on a machine where you cannot install anything, since the CSS formatter needs only a browser.

Things that can break

  • Unquoted data URIs. Beautify starts a new line after every semicolon, including the one inside url(data:image/png;base64,...), which breaks the URL. Put quotes inside the brackets first: semicolons inside quoted strings are left alone. For how data URIs work, see encoding images to Base64 in the browser, and the image to Base64 tool writes the quoted CSS for you.
  • An unclosed /* comment makes the CSS formatter drop everything after it, in both modes.
  • Minify removes the space before a colon, so div :first-child becomes div:first-child, which matches different elements.

If the CSS sits in a style block inside a page, the HTML formatter handles the markup around it. Colors for a new stylesheet can come from the color palette generator, and the rest of the formatters are in the developer tools section.

Frequently Asked Questions

Why is formatting CSS important?
One declaration per line makes a rule quick to scan and edit. It also keeps diffs in version control small, because a changed value shows up as one changed line instead of a change somewhere in a single long line.
Does the formatter support SCSS or Less?
Only partly. Nested rules are indented correctly because the formatter follows the braces, but it has no SCSS or Less parser. Line comments that start with // are not recognised and get merged into the line after them, which breaks that line, so remove them or turn them into /* */ comments first.
Will minifying CSS break my styles?
Usually not. Minify removes comments, whitespace and the last semicolon in each block, and leaves property values alone. Two cases change meaning: the space before a colon is removed, so div :first-child becomes div:first-child, and runs of spaces inside quoted strings, such as a content value, shrink to a single space. Beautify collapses those spaces in strings too.
How much does minification reduce CSS file size?
It depends on how much whitespace and how many comments the file contains. The size panel shows the exact size before and after, in bytes, with the percentage saved. Minify does not shorten colours, rewrite values or merge duplicate rules, so a file that is already compact shrinks very little.
Is this CSS beautifier safe for private code?
Yes. Beautify and Minify are plain JavaScript functions that run in your browser tab. The CSS you paste is not uploaded, stored or logged by the site, so you can format stylesheets from client projects or internal tools here.
Can I format CSS so each rule stays on a single line?
Not with this tool. Beautify always puts each declaration on its own line, and Minify puts the whole stylesheet on one line. For a one-rule-per-line layout, minify first and then add a line break after each closing brace in your editor with find and replace.