Are you fond of riding a horse? I am! To tell the truth, I tried it just once, but I definitely enjoyed it. This awesome feeling of graceful connection with a horse, a sense of trust, freedom, strength and total harmony - is something that’s hard to explain, but you know you feel so comfortable with it, that nobody in the whole universe could talk you out of experiencing it again and again. By the way, my Team Lead also likes horse riding! (you can make fun of the hat he's wearing in the comments section 🙂 )
But what would you say if you had to use a horse as a means of transport? In your everyday life. Just to get to work, for example. A hundred miles away from your home. Of course, if you are a historical character from the Iron Age, you’d be overjoyed with it. And what about today? Would you still prefer it to other means of transport? Is there any reason to shy away from the innovations, if they make your life easier?
I hope you’ve got the idea 🙂 . And yes, I’m hinting at using preprocessors instead of vanilla CSS.
In TemplateMonster website templates, the use of preprocessors is all in a day's work - Jade as a preprocessor for HTML, and SCSS as a preprocessor for CSS. In this tutorial, we shall discuss some of the CSS preprocessors and how to deal with them if you’re just a beginner in web development.
The process of developing a web project nowadays becomes more complicated, as the styles, particularly the stylesheets, are getting larger and more complex. That is why in 2007 smart people invented a way to accelerate your development by using the first version of a handful of solutions called preprocessors.
A preprocessor is a language on top of CSS that adds more functionality and gets compiled into CSS. It means that the code is written in a scripting language which is then transformed into the regular CSS.
As we know from Wikipedia, in computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers (Source: Preprocessor).
There’s a bunch of different preprocessors and post processors existing nowadays. Sass/SCSS, Stylus, Less, PostCSS, Cssnext, Rework, Stylecow, Sly, Pleeease, CSS-Crush, CSS-On-Diet, Myth - are just some of them. The first three preprocessors are still widely used, though postprocessors are getting more and more popularity as a functional addition to the preprocessors.
Sass, which stands for Syntactically Awesome Style Sheets, was the first CSS preprocessor and is still a leader now, while gets a 63% of usage. Initially, it was designed by Hampton Catlin and developed by Natalie Weizenbaum in 2007, as already mentioned. It runs on Ruby and is processed server-side. Sass has two syntaxes: the first one is the original Sass syntax, which omits brackets and semicolons, while nesting is indicated by the indents; the second is SCSS (Sassy CSS) - more similar to CSS, but still having Sass features.
Stylus, in its turn, was influenced by both Sass and Less and created by TJ Holowaychuk in 2011. Its syntax is more versatile - you can use brackets and colons/semicolons, or you can omit them. Moreover, you’re able to write your code with and without these symbols - and it will still be valid and properly compiled. Defining variables also does not require some additional signs before their name, though using $ sign is allowed.
Why it's Better Than a Horse?
Let’s dive a little deeper into the subject and review the advantages of these preprocessors, using code excerpts from the Eveprest - Multipurpose PrestaShop Theme as examples.
Variable is a mechanism for referring a value. Imagine that we have to change the same color of many elements. This is a brilliant example which is used everywhere to describe the benefits of using variables. We’ll use it too. By the way, in TemplateMonster templates, we use them throughout. There’s even a separate file called ‘_variables.scss’ where all the general variables are stored. Why is there an underscore at the beginning of the file name? I’ll mention it further, talking about partials. And now let’s see how we define a variable and use it throughout the project.For example, this SCSS code:Will be compiled to CSS the following way:
You can nest selectors in each of the preprocessors, but don’t forget that when using Sass (not in SCSS) nesting is implemented in the form of indents. In SCSS, Less and Stylus you just place the child selector inside the parent’s brackets. It works the following way:One more example of nesting:SCSSCSSSCSSCSS
Probably the most important feature of preprocessors. Mixin (from mix-in) is a set of properties and selectors describing the behavior of another selector. It’s almost the same as a function. You can use it multiple times in the project calling it whenever needed. Let’s see how it works:SCSSCSS
- Calculations and functions.
Sometimes you need to use your previously created variables, but with some modifications. For example, the margin which should be larger than the standard one, defined by the variable. Or the color which is used for the background should be lighter than the font color. Then you’re just fated to use Sass functions.Sass supports standard math operations (you can use +, -, *, /, and % operators) as well as many functions, for example, applied to colors (‘lighten’ and ‘darken’ are probably the most popular among them). You can find a full list of operations you can find here - Sass Functions. And this is how they are used in our templates:SCSSCSS
When the project is quite large, and there are lots of elements, therefore styles, it’s not that convenient to navigate through them within one file. That is the reason why partials are used. A large ‘style.scss’ file can be split into much smaller ones. These partials file names should start with ‘_’ in order not to be compiled into the corresponding .css files.They are imported into the main styles file the following way:
A set of properties defined to some selector can be inherited by another selector, and modified as well. You just need to use ‘@extend’ and the class which is going to be inherited. Then add or rewrite some properties. And voila:SCSSCSS
- Complex browser support (autoprefixer).
One of the most important and time-saving features of preprocessors is auto-prefixing. Enabling this option, you need no longer type the vendor prefixes time and time again.They will be added automatically, just as in this example:SCSSCSS
If you are not familiar with command line, that’s not a problem either. There are lots of applications to compile .scss/.less/.styl files to CSS. CodeKit, Koala, Prepros, LiveReload - are the most popular choices.
The ComparisonSo why use a horse if you can ride a car?
Preprocessors can be compared to cars, really. They speed up the process and make your life easier, though you need some time to get used to them. New versions are constantly being developed, improving the previous ones. You can choose which one is the most suitable for you, and change it to another if you have a particular reason.
I’ve tried to create the analogy between preprocessors and cars and attempted to visualize how each of these programs would look if it were a vehicle. But that was not simple. To tell the truth, the process of searching out the analogy was really tough. There was a problem finding a parallel between preprocessors’ and cars’ features or characteristics. But then it became obvious. The three most popular preprocessors can be compared to the three most popular automobiles in the world!
And suddenly the similarities began to show up, so let me elaborate on the idea:
- Sass is the leader of the three, that’s why it can be compared to a Toyota Corolla as the most popular car in the world nowadays. It’s the most simple, reliable, and efficient vehicle compared with its competitors. It’s truly one of the easiest cars to drive in its segment. And what’s amazing - all of its features can be applied to Sass.
- Less is very similar to the Volkswagen Golf. The iconic hatchback belongs to the same price category as Corolla, and they both have much in common, like Sass and Less. Nevertheless, Volkswagen hasn’t yet achieved such a predictable user experience as Toyota, but they are still very close. Less needs the lower timing for processing. Just like the Golf performs faster than the Corolla. To be fair, Golf’s top speed can be compared with the faster performance of Less. Moreover, they were both were created by Germans!
- Stylus can be compared with the Ford F-series where its robust nature always shines through. It differs from other preprocessors with its syntax, just like the F-series design totally differs from the Corolla and Golf. Moreover, Ford F-series is becoming more popular and has recently become the second most popular vehicle replacing the Golf, just like the Stylus preprocessor, is tending to overtake Less.
As we see, there are definitely some real comparisons between preprocessors and cars - and riding in a car is undoubtedly more rational and efficient than riding on a horse. Sure, if you want to experience it sometimes just for pleasure, it’s up to you. But if you want to be productive, you’re earnestly recommended to choose one of these vehicles. Decide which one is better suited to your needs and…bon voyage!
I’m hoping you won’t remain indifferent after reading this article. Even more - I’m very curious to know your opinion concerning this topic. So here are some questions for you!
Don’t hesitate to submit your answers in the comments below.
- Do you use a CSS preprocessor/postprocessor?
- Which preprocessor/postprocessor do you use?
- In your opinion, what is the future of preprocessors?