CSS Preprocessors: Are They Worth It?
CSS preprocessors promise to bring a lot of value to a development team, and indeed come packed with many features which CSS has lacked for years. You will find that many established organizations rely on preprocessors such as SASS or LESS to organize their stylesheets, and many job postings ask that you come with some knowledge of these technologies. This post is aimed at those who are less experienced in this area. I will be presenting a summary of arguments in favor of and against CSS preprocessors, and my opinion on whether they are worth using on your personal projects.
Why You Should Use CSS Preprocessors
SASS and LESS (and similar libraries) have simple aims: to make it easier for you to style your app, and to keep you from writing more code than you have to. The biggest use-case is with variables. If there are colors, widths, margins or anything else that are used in many spots throughout your project, it is a pain to put these values in so many places throughout your app. Nicer is to set a value once, and refer to it throughout your stylesheet as needed. Any time you update this value, it only needs to be changed in one place. Next, there is the ability for you to nest your selectors; that is, you can use a nested code structure to style an element along with any selectors that begin with it, as shown below.
This allows for a very natural, easy appreciation for the sselector structure, rather than having a separate entry for each selector such as “.follow-card .follow-details p”.
There are other great features that these libraries allow, but in my opinion, these are the best features, and they are also extremely easy to implement.
What’s more, SASS compiles directly into regular old CSS: no need to worry about browser compatibility.
Why You Shouldn’t Use CSS Preprocessors
Otherwise, some would say that the use of SASS or similar is superfluous, and that there are great ways to organize your CSS without the use of these libraries. Indeed, there are many ways to organize your CSS that you probably aren’t taking advantage of. The most natural way to organize your stylesheets is by grouping selectors, and specifying all attributes relevant to these selected elements in one chunk — a small rethinking of this procedure into writing as few declarations as possible, as shown below, could reduce the lines of code in your css file by a surprising amount.
Sorting selectors and declarations in a logical way, too, can go a very long way in making your stylesheets easy to navigate. At the point that you make use of these organization techniques, and if you choose to use CSS vars, it may not be worth it to you to refactor your project to use SASS.
At the end of the day there is little difference between a SASS-ified project and a well organized vanilla stylesheet. I will state though that if you are unfamiliar with it, that you should definitely refactor or build a small project with SASS, simply due to the fact that it takes so little time to learn and is so pervasive — it is simply one of the best return on investments of your time. I have refactored with SASS and found great joy in it, but in the future I will aim to go without it as well as I can.