About the CSS Formatter
Paste a minified or messy stylesheet and this CSS formatter turns it back into readable code. Each rule gets its own block, each declaration its own line, and rules inside @media, @supports or nested CSS are indented one step further for every level. If CSS has reached you squashed onto one line, this puts it back in a shape you can read, review or debug.
It works on CSS copied from a production site, a WordPress theme, a browser's DevTools or an email template, so you can find the one property you need to change. The result updates as you type, and Copy takes all of it.
How the formatter lays out CSS
The stylesheet is read token by token with the open-source js-beautify formatter, which runs inside the page. Because it understands the structure, it knows a brace that opens a rule from one that closes it, a semicolon that ends a declaration from one inside a quoted string or a url(), and a comment from code. Each block is indented two spaces per level of nesting, so a rule inside a media query sits one step in and its declarations two steps in.
The layout follows common editor conventions: a space before each opening brace, one declaration per line with a space after the colon, each selector of a comma-separated list on its own line, a space after commas inside values such as font stacks, and an empty line between rules. Values themselves are not rewritten: colours, units, numbers and the case of keywords stay exactly as you typed them.
Before formatting, the input is checked for something that looks like CSS: a selector with a block, an at-rule such as @media, or a property: value; pair. If none is found, the result box shows a warning instead of output, so you notice when you pasted the wrong thing.
Assumptions and limits
- Spacing is normalised, so margin:0 becomes margin: 0 and Georgia,serif becomes Georgia, serif. Browsers read both forms the same way.
- Comments are kept and placed on their own line above the rule they precede.
- The last declaration in a block keeps whatever ending it had: if the original had no final semicolon, the formatted code has none either.
- There is no validation. A missing closing brace is not repaired, so everything after it is indented as if it were still inside that block, which is often the quickest way to spot where the brace went missing.
- SCSS and Less that use braces and semicolons lay out well, including nested rules, variables, @include lines and // comments, but the formatter doesn't compile or check them.
Worked examples
Beautify a minified CSS line
Two rules squeezed into 70 characters on a single line come out as ten lines with an empty line between the rules, one declaration per line and the values untouched.
Input:
body{margin:0;padding:0;color:#333}.btn{background:#0a66c2;color:#fff}
Output:
body {
margin: 0;
padding: 0;
color: #333
}
.btn {
background: #0a66c2;
color: #fff
}Grouped selectors and font stacks
Each selector in the list h1,h2,h3 gets its own line, which makes long selector lists from frameworks easy to scan, while a font stack stays together in one declaration with a space after each comma.
Input:
h1,h2,h3{font-family:Georgia,serif;line-height:1.2}
Output:
h1,
h2,
h3 {
font-family: Georgia, serif;
line-height: 1.2
}A media query from a theme file
The two rules inside the media query are indented one level and their declarations two, so the nesting is visible at a glance. The media condition itself is left as written.
Input:
@media (max-width:600px){.nav{display:none}.menu{display:block}}
Output:
@media (max-width:600px) {
.nav {
display: none
}
.menu {
display: block
}
}What the formatter changes and what it leaves alone
| Part of the CSS | What happens |
|---|---|
| Opening brace { | Space before it; the block's contents start on the next line, one indent deeper |
| Declarations | One per line, with a space after the colon |
| Closing brace } | On its own line, lined up with the rule it closes |
| Selector lists a,b,c | One selector per line |
| Rules inside @media or nesting | Indented two spaces per level |
| Strings, url() and data URIs | Kept intact, even when they contain ; or { |
| Colours, units, numbers, keyword case | Kept exactly as written |
| Comments /* */ | Kept, on their own line |
How to use the CSS Formatter
- Paste your stylesheet. Put the CSS in the Input Text box; a small sample is there to start with and the Clear button empties the box.
- Read the formatted result. The Result box fills in as you type, with one declaration per line and nested rules indented.
- Copy the formatted CSS. Press Copy above the result and paste it into your editor, DevTools or a code review comment.
Frequently asked questions
How do I format a minified CSS file?
Open the .min.css file in any text editor, select all, and paste it into the input box. The formatter rebuilds the layout from the rules themselves, so a one-line file of a few hundred rules becomes a long, readable list you can search for a selector or property.
Will formatting CSS change how my page looks?
No. Only whitespace between tokens changes, which browsers ignore. Quoted strings, url() values and base64 data URIs are copied as they are, even when they contain semicolons or braces, and no value is edited.
Does the CSS formatter fix errors or validate my code?
No. It only changes layout. A missing brace or a misspelled property is formatted as it is, which often makes the mistake easier to spot because the indentation goes wrong from that point on. Nothing is corrected or reported apart from the warning shown when the input doesn't look like CSS at all.
Are rules inside a media query indented?
Yes. Every level of nesting adds two spaces, so rules inside @media, @supports, @layer or native CSS nesting sit one step in, and their declarations one step further. Nested SCSS rules are indented the same way.
Can I pretty print CSS from a website I'm inspecting?
Yes. Copy the stylesheet from your browser's developer tools or from the page source, paste it here and copy the formatted version back out. Only the text you paste is processed, so you can format a single rule or a whole file.
Is the CSS I paste sent to a server?
No. The formatter is loaded with the page and runs in your browser as you type, and the code isn't uploaded or saved. Refreshing the page puts the sample back in place of your code.
This tool runs entirely in your browser. What you enter or upload stays on your device and is never sent to our servers.