CSS is great in itself to design webpages. It involves a lot of recurring tasks and it’s difficult to manage the style sheets when the application is very huge. To avoid these recurring tasks and to write CSS in a better way, we need a preprocessor.
Preprocessors are CSS extensions. Preprocessors have all the features of CSS with additional features like variable, expression, mixin etc. A preprocessor will not do anything great in an end user's perspective but it can make development faster and easier. In the end user's perspective for performance, it can concatenate(import) all the style sheets to a single file so that all the styles for an application will be loaded in one network round-trip instead of separate requests for each style sheet.
Styles written using any preprocessor has to be converted to CSS and the compiled CSS file has to be included in the HTML page because the Browser understands CSS.
We have popular Preprocessors like LESS, SASS, Stylus. We are going to discuss Sass.
What is SASS
SASS is the acronym for Syntactically awesome style sheets. SASS was a part of another preprocessor called HAML. HAML was written by Ruby developers. Hence, SASS followed Ruby-like syntax & Ruby needs to be installed on your machine to compile SASS/SCSS files. In its third version, it got CSS like syntax and is referred as Sassy CSS or SCSS. Hence, this article is going to explain about preprocessors in general and SCSS.
Advantages
- It makes your Application easy to maintain by avoiding all recurring tasks. Usage of additional features like variable, expression, nesting, inheritance, mixins, partials makes it more comfortable than CSS.
- SASS provides many useful functions to manipulate colors and other values.
- SASS provides advanced features like control directives for the libraries.
- SASS allows to format and customize the compiled CSS file.
Pre-requisites
To use SASS, we need to install Ruby first. It’s a single click process. Make sure to check “Add Ruby executable to your PATH” option during installation. It makes Ruby available globally. However nowadays there are many editors including Visual studio 2013 Update 2 & advance that provides built-in support for SASS. Hence, if you can’t install Ruby also, there are other alternatives.
Gem will be installed with Ruby. Open command prompt with Ruby & install SASS with Gem by following command.
gem install sass
If any error occurs,
sudo gem install sass
For converting SCSS files to CSS file,
sass <sourcesassfilenamewithpath>.scss : <destinationcssfilenamewithpath>.css
sass sassfiles /style.scss : stylesheets/style.css
SASS vs SCSS
- SCSS is the third version of SASS. They are just syntactically different from each other.
- SASS
○ SASS code is compact, it needs fewer key strokes. We don’t need braces, semicolons.
○ Indentation plays a major role. Hence, it forces coding standard. - SCSS(Sassy CSS)
○ More expressive
○ More readable
○ Easy to learn
○ Integration with css is easy. You can directly use the css code from Plugin. Css to SASS conversion tools are not 100% correct.
| Sass | SCSS |
| ★ // Variable !primary-color= hotpink ★ // Mixin =border-radius(!radius) -webkit-border-radius= !radius -moz-border-radius= !radius border-radius= !radius ★ .my-element //using variable color= !primary-color width= 100% overflow= hidden ★ .my-other-element // using mixin +border-radius(5px) | ★ // Variable $primary-color: hotpink; ★ // Mixin @mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; border-radius: $radius; } ★ .my-element { // using variable color: $primary-color; width: 100%; overflow: hidden; } ★ .my-other-element { // using mixin @include border-radius(5px); } |
SASS - SCSS conversion
SASS files can be converted to SCSS and vice versa.
- Convert SASS to SCSS
$ sass-convert style.sass style.scss - Convert SCSS to Sass
$ sass-convert style.scss style.sass
SASS Script - Interactive shell
To launch the interactive shell, execute following command in command prompt opened with Ruby,
sass -i
>> 1px + 10px
11px
>> #777 + #777
#eeeeee
>> 10px/5px
2
SASS With Grunt
Grunt is a JavaScript runner that reduces our recurring task for deployment like concatenation or bundling, minifying css or js files etc.
Follow the steps to work with Grunt & SASS
- Install NodeJS to get npm (Node Package Manager). Download NodeJS and install it.
- Install grunt.
If project and package.json file already exists, go to project root directory and execute the following command.
npm install → It will install all the devDpendencies mentioned in package.json.
If project is created from scratch or does not have package.json file already.
npm init → It will ask some questions and create package.json as per your responses.
npm install grunt --save-dev → It will install grunt and add the same in devDependencies section of package.json file.
npm install grunt-contrib-sass --save-dev → It will install grunt-contrib-sass.
npm install grunt-contrib-watch --save-dev (optional) - Create Gruntfile.js in project root directory where we need to define tasks for Grunt task runner.
Paste following in Gruntfile.js.Now in cmd, you can go to your project root directory and execute the following Grunt SASS,- module.exports = function(grunt) {
- grunt.initConfig({
- pkg: grunt.file.readJSON('package.json'),
- sass: {
- dist: {
- files: {
- // destination file : source file
- 'style/style.css' : 'sass/style.scss'
- }
- }
- }
- });
- grunt.loadNpmTasks('grunt-contrib-sass');
- grunt.registerTask('default',['sass']); // Register default grunt task
- }
Or
grunt
It will compile the SCSS file and create css file for you.
Sass features
| Variable | $ |
| Nesting | {{}} |
| Inheritance | @extend |
| Mixin | @mixin @include |
| Expression | $ function () + |
| Partial | _ @import |
| Function | |
| Directive |
Variable ($)
Mostly used values can be kept in the variables. Let’s say, you want to give a particular color to some elements along with some other styles in your page. Say in the future, you want to change this color. If you are using css, you need to change this color value for all the elements separately. If you are keeping this color value in some variable and using it, you just need to change this variable value.
| SCSS | CSS |
| $cre-bs-font-color: hotpink; .cre { color: $cre-bs-font-color; // some other styles } .bs{ color: $cre-bs-font-color; // some other styles } | .cre { color: hotpink; } .bs { color: hotpink; } |
Expression
SCSS allows to use arithmetic operators (+, -, *, /, %) for the expression, to provide calculated value for styles.
- SCSS doesn’t evaluate plain css. E.x. font: 15px/12px won’t evaluate as it indicates line height font color.
- Use a variable or parenthesis for expressions.
- Width: 100px/2 // This won’t evaluate, but followings will evaluate
- $width: 100px; width: $width/2;
- Width: (100px/2)
- Parenthesis for list values doesn’t evaluate.
- font:(italic bold 10px/8px) // Won’t evaluate
- Use function, + for expression.
- margin-left: 5px + 8px/2px; // This will evaluate

Kuppurasu NagarajPosted Jun 6, 2016, 12:09 PM
Nice Sharing..
Sonu ChaudharyPosted Jun 6, 2016, 9:52 AM
good one...
Debasis SahaPosted Jun 6, 2016, 12:22 AM
Good One..
Debasish TutuPosted Jun 4, 2016, 5:30 AM
nice Article Rasmi .Keep up good work
Sanjay GuptaPosted Jun 2, 2016, 8:49 AM
Nice and useful. A must read for front end developer.
Debendra DashPosted Jun 2, 2016, 6:07 AM
good one,,,,,
Vignesh ManiPosted Jun 2, 2016, 5:58 AM
Good one
RajaPosted Jun 2, 2016, 2:49 AM
Nice Article....
Ketak BhalsingPosted Jun 2, 2016, 12:44 AM
Helpful information....
NitinPosted Jun 1, 2016, 11:38 PM
Good show
Thiruppathi RPosted Jun 1, 2016, 10:15 PM
Nice Article..
Debasis SahaPosted Jun 1, 2016, 2:07 PM
Nice one..