← All blog posts

Css best practices and common mistakes

WhiteUI · A plain-English guide to css formatter

The CSS Formatter takes CSS that is hard to read — everything on one line, uneven spacing, missing line breaks — and rewrites it with consistent indentation and one declaration per line. It does not change what the CSS does; it only changes how it looks, so the same rules style the same elements after formatting.

Best practices and common mistakes

The best practice is boring and it works: use real numbers, not round guesses. A guess that is 20% out produces an answer that is 20% out, and no tool can fix that from the inside.

The second practice is to save or note the inputs alongside the result. A result without its inputs is hard to reproduce a week later, and you will not remember whether you used last month’s figure or this month’s.

The most common mistake is formatting only the file you are looking at and leaving the rest of the project in the old style. Pick one style — two spaces or four, one declaration per line — and apply it everywhere, or the next person to open the project will still have to guess.

Worked example

Paste this single line: .card{color:#333;padding:16px;margin:0 auto;width:300px}. The formatter spreads it out so each declaration sits on its own line, indented under .card, and the closing brace sits on its own line too. Reading the result takes seconds, because your eye can follow one property at a time instead of untangling a whole rule from one long line.

In a real file the gain is larger. A stylesheet of 340 lines, pasted in as a few long lines, comes back as 340 readable lines with matching indentation. If you then spot a duplicate padding rule on line 212, you can fix that one line without touching anything else.

Method: read each rule, put each declaration on its own line, indent declarations by one level, and keep selectors and braces on their own lines.

At a glance

Two parts of the same result, shown side by side so the balance is visible at a glance. The exact figures are in the worked example above.

Formatter or minifier: which one you actually need

A formatter and a minifier do opposite jobs to the same file, and mixing them up is why formatted code sometimes ends up back on one line. A formatter spreads code out — one declaration per line, steady indentation, braces on their own lines — so a human can read it. A minifier squeezes code down, removing line breaks, indentation and comments, so a browser fetches it faster. The rule in the worked example shows the difference in miniature: it is 56 characters crammed on one line and 75 characters once formatted, with the 19 extra characters all spaces and line breaks. Nothing about the rule changed; it just got easier to read.

Use the formatter while you are writing and fixing CSS, and the minifier on the copy you publish. For one small stylesheet, formatting alone is enough, because the file is small either way. For a site with several stylesheets, keep the formatted files as your source and generate minified copies when you publish, so you never edit the crammed version. The rule is simple: if a human will read it next, format it; if only a browser will fetch it, minify it.

What to check before you format a file

First, check the CSS is valid. A missing brace or semicolon confuses a formatter just as it confuses a browser, so fix the error first and format second. Second, check for an unclosed comment: everything after a stray /* is treated as comment, so a file that looks half-empty after formatting usually has one. Third, look for duplicate properties. Formatting lines them up where you can see them — the worked example above has a duplicate padding rule hiding on line 212, and it only becomes obvious once each declaration sits on its own line.

Fourth, check the page still looks identical after formatting. Open it in a browser and compare it with the unformatted version; if anything moved, the problem is in the original CSS, not the formatting. Fifth, format the whole project, not one file. A single tidy file in a messy project still leaves the next person guessing, and a consistent style across files is worth more than a perfect single file.

Frequently asked questions

Does formatting change how my page looks?

No. Formatting only changes spacing and line breaks. The selectors, properties and values stay exactly as you wrote them, so the page renders the same way.

Can I format CSS that has an error in it?

You can try, but a missing brace or semicolon can confuse any formatter. Fix the error first, then format, and the result will be clean.

Do I need to install anything to use it?

No. The formatter runs in your browser. Paste the CSS, copy the result, and nothing is uploaded or stored.

More guides on the CSS Formatter

Try it yourself

A free formatter that turns messy, cramped CSS into clean, readable code you can scan at a glance.

Open the CSS Formatter