Nobody gets excited about CSS formatting. It sits in that category of unglamorous work that only becomes visible when it's missing — like backups or good variable names. But messy stylesheets slow reviews down, hide bugs, and make merge conflicts miserable. Spending five minutes to get formatting right is one of the cheapest wins in front-end work.
What "formatting CSS" actually means
Formatting is about the shape of the code, not its behaviour. The browser doesn't care whether your CSS is on one line or fifty — it parses the same rules either way. Formatting is entirely for humans. It covers things like:
- Consistent indentation (two spaces vs four vs tabs)
- One declaration per line, or several packed together
- Spacing around colons, braces, and after commas
- Whether properties are alphabetised or grouped by concern
- Blank lines between rule blocks
The goal is predictability. When every file follows the same shape, your eyes learn where to look and diffs stay small.
When you actually need to format it
A few situations come up over and over.
You pasted minified CSS
You copy a snippet from a build output, a browser dev tools panel, or a third-party library, and it's all crammed onto one line. Trying to read .btn{padding:8px 16px;border:0;background:#0b6;color:#fff;border-radius:4px} in a debugger is painful. Expanding it makes the structure obvious.
Cleaning up inherited code
You open a legacy stylesheet where three developers used three different styles. Running a single formatter over the whole file gives you a clean baseline before you start changing anything.
Before committing
Consistent formatting keeps pull requests focused on real changes. If half your diff is whitespace noise, reviewers miss the actual logic.
Common gotchas
Formatting sounds trivial until it bites you. Watch for these.
Formatting is not linting
A formatter fixes how the code looks. A linter (like Stylelint) catches problems — duplicate properties, invalid values, unknown units. They complement each other. Don't assume that pretty CSS is correct CSS.
Whitespace in shorthand values matters
Most spacing in CSS is cosmetic, but not all of it. In calc(), the operators need surrounding spaces: calc(100% - 20px) works, calc(100%-20px) does not. A naive reformat that strips spaces around - can silently break your layout. Good formatters know this; hand-editing sometimes doesn't.
Custom properties and comments
Some tools mangle CSS custom properties (--brand-color) or reorder declarations in ways that break the cascade. If you rely on property order for overrides within the same selector, blind alphabetising can change the result. Test after reformatting anything you didn't write.
Trailing commas and modern syntax
Newer features like nesting, @layer, and container queries confuse older formatters. If your output looks wrong, check that the tool understands the syntax you're using before assuming your CSS is broken.
A short worked example
Here's a chunk of real-world CSS in the state you often find it — minified, inconsistent, hard to scan.
.card{display:flex;padding:16px;gap:12px;border:1px solid #ddd;border-radius:8px}.card__title{font-size:1.25rem;margin:0 0 8px;color:#222}.card--featured{border-color:#0b6;background:#f4fff9}
.card__body p{line-height:1.6;margin:0}Now the same rules, formatted for humans:
.card {
display: flex;
padding: 16px;
gap: 12px;
border: 1px solid #ddd;
border-radius: 8px;
}
.card__title {
font-size: 1.25rem;
margin: 0 0 8px;
color: #222;
}
.card--featured {
border-color: #0b6;
background: #f4fff9;
}
.card__body p {
line-height: 1.6;
margin: 0;
}Same bytes to the browser, wildly different for the developer. You can now scan each block, spot the featured modifier at a glance, and add a property without hunting for the right semicolon. If you just need a quick one-off cleanup without wiring up tooling, paste it into a CSS Formatter and copy the result back.
Automate it so you never think about it
Manual formatting doesn't scale across a team. The moment two people disagree about tabs, you've lost. Push the decision into tooling.
Prettier is the common default. Add a config so the whole team shares one style:
// .prettierrc
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": false
}Then run it on your styles:
npx prettier --write "src/**/*.css"Hook it into a pre-commit step (with something like Husky and lint-staged) and formatting becomes invisible. Files get cleaned as they're committed, diffs stay honest, and you stop arguing about whitespace in code review.
Pick a style and commit to it
There's no objectively correct format. Two spaces or four, alphabetical or grouped — it barely matters. What matters is that everyone on the project uses the same one. The value of formatting is consistency, and consistency only exists when it's enforced, not suggested.
The practical takeaway
Treat formatting as plumbing. Get it working once, automate it, and forget it. Use a formatter for quick cleanups of minified or inherited code, pair it with a linter for correctness, and stay alert to the handful of places where whitespace genuinely changes behaviour — calc() being the classic. Do that, and your stylesheets stay readable, your reviews stay focused, and the boring problem stays solved.
Photo by Florian Olivo on Unsplash