Table of Contents
First of all, what is a CSS preprocessor?
Internet browsers only understand and listen to CSS, so it cannot be simply replaced by a completely new language. However, it does have certain limitations, which might not feel significant in small projects, but do take their toll when you’re dealing with huge stylesheets. To work around this problem, developers use CSS preprocessors. In their essence, preprocessors are programs that have their own unique syntax. After you write your code, they compile it to pure CSS.
The reason we’re using CSS preprocessors is to add extra functionalities that CSS wouldn’t otherwise have. For example, you could have nesting or inheritance selectors, as well as mixins (reusable declaration bundles). The handy additional features let you be more efficient in your daily work and provide you extra scalability.
Which CSS preprocessor should you use?
Choosing a CSS preprocessor is not an easy feat. Just like with most information technologies, you have a variety of choices, each with its own advantages and a passionate user base. To make a choice, you need to know your options, recognize their strengths, and understand differences.
Sass vs. SCSS
First introduced in 2006, Sass (Syntactically Awesome Style Sheets – a nice play on the official name of CSS) is considered to be the pioneer of CSS preprocessors. No wonder it is also the most popular! Sass is also used in Bootstrap 4, which is currently one of the most widely used front end frameworks in the world and thus a prominent trendsetter in the IT world.
As of now, it has two separate syntaxes: Sass and SCSS. While only the first one existed in the first versions of the preprocessor, the team got worried it might be a bit too different from plain CSS. Therefore, they presented a new syntax called SCSS (Sassy CSS) in the third version. The files can also have either an .sass or an .scss extension.
The main difference between Sass and SCSS is that the latter uses semicolons and brackets in the same way CSS does. Sass, on the other hand, does not – plus, it uses an equals sign instead of a colon for assignment. While SCSS is now officially the main syntax of the Sass CSS preprocessor, there are no plans to ever deprecate the original version. Some developers claim it’s actually more readable due to being more concise. However, SCSS is much easier to both learn and integrate to CSS.
Comparing Less vs. Sass, we will see they are a lot alike in their basic functionalities. Both of them allow nesting, importing and using variables. However, in Less, you can also hoist variables and extract certain components from a color that you could not with Sass – namely, hue, saturation, luminocity, and brightness. On the other hand, Sass allows you to use if statements and interpolate them in selectors and property names. Its syntax also contains ternary operators and nesting collectors – and Less, unfortunately, does not.
Another little disadvantage of Less is that is uses the @ symbol for variable declaration (Sass uses the $ sign instead). However, in CSS, @ can also be used for keyframes and media queries. This might cause a little confusion when reading the code.
Another CSS preprocessor called Stylus appeared on the market in 2010. While it has a noticeably smaller user base than Sass or Less, it’s worth to have a look at. Stylus is written in Node.js and is therefore an attractive option to those already familiar with the environment. It also has useful built-in functions and mixins.
What makes Stylus distinct first and foremost is its flexibility: colons, semicolons, and commas are all optional. Furthermore, you don’t need curly braces to define code blocks: instead of symbols, Stylus uses indentations for that. As for variables, you may use the $ sign like in Sass… But you might also skip it. No big deal.
All of this lets you type less and have a cleaner code. Unfortunately, some developers actually find this flexibility to be a flaw. Having no clear identifiers makes the code hard to read and understand, especially in bigger projects. What you get is a freedom-caused anarchy, so to speak. If we compare Stylus vs. Sass, the latter clearly has an edge with its easily readable code.
PostCSS was initially released in 2013 and is currently on version 7. You can use it with both plain CSS syntax and that of the preprocessors mentioned above. What gives it a slight edge over Sass, Less, and other CSS preprocessors is modularity. It allows you pick and choose the features you need, as well as to work faster. No wonder its popularity is rising rapidly – there have even been public declarations that it might be used in the upcoming version of Bootstrap.
What else there is to know?
CSS preprocessors add a necessary step of compilation, which can slow down both development and debugging. The final generated CSS file can also be rather big as well.
However, the advantages far outweigh the disadvantages at this point. Variables, mixins, nesting, multiple extra functions – all of these features make writing and maintaining a code much more convenient. Choose a preprocessor you feel the most comfortable with and enjoy the extended functionalities of CSS now!