CSS Property Sorting: Why It Matters and How to Set It Up?
When working with CSS or styled-components, it’s easy to get lost if properties are written in a random order. It makes code harder to read, review, and refactor — and increases the chance of mistakes.
One of the easiest ways to keep things clean is to sort CSS properties logically.
.button {
color: white;
display: flex;
padding: 12px;
border-radius: 8px;
background: #000;
font-size: 16px;
}
.buttonTwo {
background: red;
font-size: 20px;
display: flex;
border-radius: 20px;
padding: 18px;
color: green;
}
.buttonThree {
font-size: 26px;
padding: 20px;
color: red;
display: flex;
border-radius: 30px;
background: yellow;
}
Properties are mixed — visual, layout, fonts. It’s hard to grasp the structure of the component.
.button {
display: flex;
padding: 12px;
border-radius: 8px;
background: #000;
color: white;
font-size: 16px;
}
.buttonTwo {
display: flex;
padding: 18px;
border-radius: 20px;
background: red;
color: green;
font-size: 20px;
}
.buttonThree {
display: flex;
padding: 20px;
border-radius: 30px;
background: yellow;
color: white;
font-size: 26px;
}
Now everything is structured:
display
, padding
, margin
border
, background
, box-shadow
color
, font-*
, line-height
The best way is to automate it:
Use the stylelint-order
plugin to define your property order.
npm install stylelint stylelint-order --save-dev
Sample .stylelintrc.json
:
{
"plugins": ["stylelint-order"],
"rules": {
"order/properties-order": [
"display",
"position",
"top",
"right",
"bottom",
"left",
"margin",
"padding",
"width",
"height",
"background",
"border",
"box-shadow",
"color",
"font-size",
"line-height"
]
}
}
If you’re using Tailwind or styled-components, prettier-plugin-tailwindcss
automatically sorts utilities.
npm install -D prettier prettier-plugin-tailwindcss
Sorting your CSS properties is a small habit with a big payoff:
🔧 Automate the sorting — and forget about messy styles forever.