Ordering CSS Declarations
blog.jim-nielsen.com
blog.jim-nielsen.com
My ordering is by my mental model of "impact" of the declaration. Generally: Display -> Position -> Box Model -> Text/Font -> Background -> Misc.
/* Example from css file i have open */
.officeBlock .email {
display: inline-block;
padding: 14px 10px 8px;
background-color: #E0EFFE;
border-radius: 50%;
transition: 0.25s ease-out;
}
Subjectively it's helpful for quickly understanding style blocks, particularly complex ones. Is the time investment a net positive? Possibly. I'd suggest it is worth trying on a project to see if it helps when you return to it months later, at least.I order every property in each bucket by their importance.
- Hacks: reserved for very special cases that solve an issue. I keep this at the top so it's the first thing I see for a class (e.g. box-sizing, -webkit-touch-callout, pointer-events, outline for debugging, etc...)
- Layout this usually means display, position, box-model.
- Decoration: this usually means border, background, text
- Animation: this usually means transitions, non-layout transforms and animations
- Misc: Anything out of the ordinary. Related media queries (soon to be replaced by container queries), scss nested elements, etc...
Here's what that gave us: crummy PR reviewers get a silly thing to complain about, developers get an extra thing to think about
Here's what that didn't give us: any reduction in bugs, any meaningful improvement in code legibility
I'm firmly in the camp of "order properties as needed."
Exact same experience working on a team that had this as a standard. It's absolute nonsense. It can be enforced with CSS linting at this point... but why? Never in my decade of writing CSS have I ever ever wished that the declarations in a codebase were alphabetically sorted for any reason. If your class declarations are that large in the first place, you should be breaking things down better.
One correction to the article I want to make is that shorthand/longhand properties are more naturally alphabetized by placing the shorthand property first (rather than the opposite, as suggested by the author), so overriding them shouldn't ever be a problem.
Ordering the properties at all probably isn't hugely important but if some declaration has 15+ properties, I like that I can zero in on the one I'm looking for rather than scanning each property or having to Ctrl+F to find what I'm looking for.
with more than 2 people and no automation or lint rule, it will inevitably not be enforced. and once you can't trust the codebase to be consistent, the usefulness of having a rule like that is gone.
alphabetical seems less intuitive to me but it at least has 100% objective answers to every question about what the order should be. no ambiguity and it's probably easier to automate or lint. if the choice is alphabetical, with automation, vs. wild-west, i'd take alphabetical.
- by category (display, position, etc)
- outside in
Then onto a variety of team preferences. Then I didn’t touch CSS for several years, as my “full stack” career became increasingly backend focused.
A previous team during that period had very strong (and sometimes idiosyncratic) code style preferences. I eventually cobbled as much of it as possible in an effort to remove those discussions from review so we could focus on more substantive discussion. But some of their preferences sank in and became my own. Of those less idiosyncratic, alphabetically ordering object properties (although I still prefer to make meaningful exceptions, eg for identifying keys or making the most important assignment most prominent) and aligning values really stuck. Both of which I had previously strongly disliked.
Anyway, when I left that job and returned to web dev, my instinct was: I want CSS properties to be alphabetized, but I’m sure it’ll never work. But after trying it, I’ve found it almost always does work!
The article gets at all of the cases I’ve encountered where it doesn’t, so I’ll defer to it. But I did want to add an anecdata as a previously skeptical convert.
Edit to add: and yes I agree with all of the comments saying this (or whatever ordering and any other code style preference) should be automated. Not just because I agree that improves the quality of review and diffs and reduces cognitive load, but also because meaningful exceptions are really hard to keep in mind under the best circumstances. Tools are not just there to do the chore, they’re also there to catch mistakes.
Ordering from the outtermost properties to the innermost is both intuitive to write and pleasant to read. And if you're still new to CSS, it can help you to better understand and memorize the parts of the box model.
Alphabetical ordering sounds hilarious
Where it would be hilarious is at a selector level. lol. [edit] (that is, unless you follow a bem-like methodology. non-lol.)