Advantages of CSS Preprocessors
Author : TechAffinity 14th Sep 2017
Writing direct CSS on style sheets is good for small web applications, but when you are developing a large user interface that requires heavy style sheets CSS preprocessor is the best solution. CSS preprocessors are special CSS files that contain variables, functions, mixins and other functionalities which are not possible with basic style sheets. They offer an easy solution that enables scalability with efficiency. Now let us look at various advantages of using CSS Preprocessors.
1. Ability to add variables, mixins, functions, etc
Basic CSS is direct and offers the least flexibility. With CSS Preprocessor, you can add variables and functions brings a new dimension and scope to CSS which facilitates easier and efficient development. It also makes your code more organized and clean.
2. Join Multiple Files
CSS Preprocessors offers a special functionality of joining multiple stylesheets into one. You can create different files for each screen or pages and then import them all in the main CSS file. Now, only the main file needs to be imported on the website and internal files will be read from the server.
3. CSS Preprocessor Helps You Avoid Repetitions
One of the biggest drawbacks of basic CSS is that you need to write everything multiple times for each object. This is totally against the principle of programming languages. This is where CSS Preprocessors are a time saver. You can write common styles once that can be imported instead of re-writing. Here is an example.
Basic CSS
.maindiv{
Margin:5px;
Padding:2px;
border:1px double;
}
.subdiv1{
Margin:5px;
Padding:2px;
border:2px double;
}
.subdiv2{
Margin:5px;
Padding:2px;
border:3px double;
}
Now the same code using a CSS Preprocessor
.maindiv{
Margin:5px;
Padding:2px;
border:1px double;
}
.subdiv1{
.maindiv;
border:2px;
}
.subdiv2{
.maindiv;
Border:3px;
}
As you can see, instead of re-writing the same code thrice, we are just calling the main style and adding new changes. This will save you a lot of time and space.
4. Nested Syntax
Nesting of classes in CSS makes it easy to target DOM elements and saves you time. Nesting also makes it tremendously easy to edit CSS files. Here is an example of nested CSS.
body {
.header {
h1 {
font-size: 28px;
}
h2 {
font-size: 21px;
}
}
}
This will translate into the following CSS
body .header h1 {
font-size: 28px;
}
body .header h2 {
font-size: 21px;
}
Now if you want to change the class name “header” to something else, it has to be done only once. The same in basic CSS would require changes in multiple places.
Also Read: Responsive Testing Tools
5. Less Time to Code
You can avoid repetitions while using CSS preprocessors and also it makes your code more organized. The combination of these two will save you a lot of time in coding. You will be able to write the same amount of styles in a much lesser time which will be easier to scale too.
6. Darken & Lighten functionality
CSS Preprocessor has many built-in functions and one among them is Darken and Lighten functions. You can just mention darkening with the color code and also the percentage. This was not possible with basic CSS. Here is an example
h1 {
color: rgb(230, 140, 230); // color: #e68ce6;
color: darken(#123456, 10%); // color: #091a2c;
color: fade(#123456, 50%); // color: rgba(18, 52, 86, 0.5);
}
Now that we have understood the benefits of CSS preprocessors, let us look at some of the most popular ones available today.
SaSS
SaSS is an 8-year-old open-source CSS project which is the most used CSS preprocessor today. It offers 2 different syntaxes to write the CSS namely, Sass or SCSS. Sass requires Ruby to compile.
Less.js
It is easily the best competitor for Sass and it runs on Node. Getting started with Less is quite easy, just download the Less.js JavaScript file and integrate with your HTML or use a public CDN for importing. With Less, one can use variables, mixins, functions, etc and it also offers a unique way to use looping.
Some of the other popular CSS Preprocessors are Stylus, CSS-Crush, Myth and Rework.