Programming

Organizing and Sorting CSS

Organizing and Sorting CSS

Compared to other web-related languages, CSS is relatively straightforward and easy to write. But at the same time, it is also hard to organize, particularly when it comes to codes that consist of thousands of lines.

Recommended Reading:TIPS FOR HOW TO WRITE BETTER CODE

Technically, CSS has no restrictions when it comes to ordering properties. The following example…

… will output the same result as the following:

But as we’ve mentioned previously, being organized will help your teammates easily maintain your codes. Sorting CSS codes, however, requires a lot of cutting & pasting, and thoughts on how to order it. And that’s where CSSComb comes in handy.
How To Use CSSComb
CSSComb is a sorting utility for CSS that is built by Slava Oliyanchuk. CSSComb supports CSS2 to CSS4, and is available in many popular code editors such as TextMate, Coda, Notepad++ and Sublime Text, as a plugin or an extension.

If you are using Sublime Text, CSSComb can be installed easily via the Package Control. Once installed, you can sort the CSS properties in several ways:

Hit the default key combination: Shift + Ctrl + C.
Right-click and select: Sort via CSSComb option.
Open Command Palette – Command + Shift + P and select Sort via CSSComb.
In this example, we have the following style rule.

The style rule above works, and there is nothing wrong with it, except that it’s a bit unorganized. Now, after running CSSComb, the properties are sorted in the following order.

 

Above is the default ordering rule in CSSComb, but if you don’t want to order it like that, you can customize the order, by going to the Preferences > Package Setting > CSSComb menu, and setting a new order rule under Sort Order – User.