ХабрПостыНовостиМегапосты?

Happy Potter and the Order of CSS


So, first of all — this is not a common battle. It's not about CSS versus CSS-in-JS, not atomic CSS versus BEM, not LESS vs SASS. This battle is about THE ORDER.


This battle is between ORDER and CHAOS. And between ORDER and,… another ORDER. In short — about a better way to sort CSS properties. No more, no less.


In short — there are 3 ways to do it:



From now let's forget about the first way. It's not the way.

2012


There was a good poll from css-tricks, and here are the results:



It's 2019 already, what was changed?


The difference between alpha sort, ABCSS as it known, and a grouping is quite sound, but it's better to make it clear


Initial state


Lets start with an ideal CHAOS from this prettier issue


.wrapper {
    top: 20px; /* is it absolute? fixed? You begin searching downwards */
    margin-left: 20px; /* Any other margins applied? */
    display: flex; /* How is this flexed?  searching for justify / align / flex rules */
    position: absolute; /* ah absolute */
    height: 100%;        /* and width? */
    margin-bottom: 20px;
    border-radius: 5px;  /* Is there even a border? */
    color: red;
    justify-content: center;
    margin-left: 2px;
    left: 0px;
    width: 100%;        /* and height? */
    border: 1px solid red;
}

Beautiful? That's how common CSS looks like after a few years without any control.


ABCSS


Sort it alphabetically


.wrapper {
    border: 1px solid red;
    border-radius: 5px;
    color: red;
    display: flex;
    height: 100%;
    justify-content: center;
    left: 0px;
    margin-bottom: 20px;
    margin-left: 20px;
    margin-left: 2px;   /* ouch? */
    position: absolute;
    top: 20px;
    width: 100%;
}

It just helped notice that we had two margin-lefts. It would be easier to add a new property next time, especially if all the sorting is done automatically on stylelint level.


The main advantage of alphabetical sorting — easy property location. You are just scanning table, like a glossary, and yep, here is the property I am looking for.

The main disadvantage — height goes before width, left goes before top and there is some distance between them. Alphabetical sorting is mixing context.


I've used some online tool to make it. The vast amount of online tools, plugins for IDEs and CLI tools support alpha sort out of the box. — easy to use.


Groups


Sort it in groups


.wrapper{
    position: absolute;
    top: 20px;
    left: 0;

    display: flex;
    justify-content: center;

    width: 100%;
    height: 100%;
    margin-bottom: 20px;
    margin-left: 20px;
    margin-left: 2px;

    color: red;

    border: 1px solid red;
    border-radius: 5px; 
}

This time it's more about separation of concerns — all properties are grouped(clustered) into buckets by a sense. The ordering principle is known as "Outside-in"



And the problem — there is more than one standard around it. To be more concrete — more than five. And that's a problem. Or not?

And, yet again — sorting is done by a tool, CSSCOMB this time.


The main advantage — all properties are grouped by common sense. All flex properties — together. All position, left, right, top, bottom — together.



This requires some clarification:
  • with alpha sort — you are looking for a beginning of the line, pointing beginning and the end of "your" first letter. Then you are going thought this subblock and that's done. That's basically O(n). After some training — O(n/2), as long you will know "where" look for a prop.
  • with group sort — you know "where" to look for a prop — groups have a constant position, and then go through small subset, and that's done. That's basically — O(3), not "n".

The main disadvantage — you have to learn how to separate conserns. 99% respondents said that this is a big problem — to write grouped CSS, but it's a tool job, not yours. And, yeah, there is a more than one standard, but it's not a problem with then ordering is done by a tool.


The Poll



So, I did some research around this problem.



Best reasons to use groups:



Some reasons not to use



Reasons to use alphabetical:



So, TLDR



It's actually a bit unexpected, that such opinionated thing is not well established and defined, and I have to write this article.


Fun fact — groups and alphabetical order could be besties, for example to lint your module imports, like eslint-plugin-import
There are groups first, and alphabetical ordering is used only inside one group.


So far I heard quite reasonable arguments pro grouping, but not a single (reasonable for me) pro alpha sort.


See also:



PICK YOUR SIDE, and explain your position. I've got some popcorn to share.

@kashey
25.03.2019 15:37 UTC
Первоисточник