Basics of SASS

Basic of SASS

Since Superman has extra power than a normal man, similarly SASS is a super-powered version of CSS. It is actually meant to make CSS more dynamic, organized and productive.

SASS is Syntactically Awesome Stylesheets. It's a CSS Preprocessor. It is written in Ruby and distributed by the Ruby package manager, RubyGems. However it can be implemented with other languages as well, like PHP, Java and C.

SASS consist of the following two syntax:

  1. SASS: It contains indented code without braces and semicolons
  2. SCSS: It is indented code with braces and semicolons

One can use either syntax depending on their practice. People who are familair with writing CSS have experienced with writing brackets and semicolons, so they can use SCSS.

Variable

Suppose you have more than 100 CSS files in your project and you want to change the text link color in the entire project. What you will do is, find and replace in the entire project using Dreamweaver or eclipse. It will be a tough job if someone has used the text color Red, #FF0000 or #F00.

In that scenario, variables are useful.

In SASS, we can create a variable for the text link color using $ and use it across the website. So if we want to change it, it requires only one change.

Example 1

  1. $text-link: #FF0000;  
  2. a   
  3.    color: $text-link;  

  4. nav a {  
  5. color: $text-link;  

 

Hierarchy

Hierarchy is an important feature of CSS. However it is a tedious job to maintain a long chain of hierarchy. SASS makes it easier. Child elements are added into parent elements, so that a structure can be easily understood. Hierarchy can be easily maintained in SASS.

Example 2
  1. CSS
  2. .main-container {  
  3.    width: 960px;  
  4.    margin: 0 auto;  
  5. }  
  6. .main-container .left-column {  
  7.     float: left;  
  8.     width: 600px;  
  9. }  
  10. .main-container .left-column .left-column-heading {  
  11.     font-size: 20px  

  1. SASS
  2. .main-container {  
  3.    width: 960px;  
  4.    margin: 0 auto;  
  5.   
  6.    .left-column {  
  7.       float: left;  
  8.       width: 600px;  
  9.       .left-column-heading {  
  10.           font-size: 20px  
  11.       }  
  12.    }   

 

Mixins

Most of the CSS3 features are browser-specific and requires browser-specific prefixes. That creates a duplication of code in CSS. Mixins acts as a function; we just create them once then call them multiple times using a different parameter. Let's look at the following example:

Example 3

  1. @mixin border-radius($radius) {  
  2.    -webkit-border-radius: $radius;  
  3.    -moz-border-radius: $radius;  
  4.    -ms-border-radius: $radius;  
  5.    border-radius: $radius;  

So wherever we want to apply a border radius then only one line will be required and we can pass any ineteger value as the parameter.

  1. .box { @include border-radius(10px); } 

Similarly we can use mixins for box-shadow, background image, text-shadow and so on.

Import

If we write the entire CSS in a single file then it becomes too lengthy and it will be hectic to maintain. Instead of this, we can separate this file into multiple files like header, footer, nav, font and so on. We can easily import this files using @import. The main benefit of SASS is that it compiles one single time though you have divided it into multiple files.

Example 4

  1. Reset.scss
  2. body, div, span, p {  
  3.     margin: 0;  
  4.     padding: 0;  
  5. }
  6.   
  7. Base.scss  
  8. @import Reset.scss  
  9. body {  
  10.     color: #000;  

 

Inheritance

We can reuse the code using the Inheritance property. SASS has the same inheritance concept as in OOP. It allows us to share a set of CSS properties from one selector to the other using @extend.

Example 5

  1. .message {  
  2.   border: 1px solid #ccc;  
  3.   padding: 10px;  
  4.   color: #333;  
  5. }  
  6. .success {  
  7.   @extend .message;  
  8.   border-color: green;  
  9. }  
  10. .error {  
  11.   @extend .message;  
  12.   border-color: red;  

 

Operators

SASS supports math operators like:

+, -, /, * & %

That is also a useful feature and helps to make responsive websites.

Example 6

  1. $container-width: 960px;  
  2. $font-size-normal: 16px;  
  3. .container {  
  4.     width: $container-width;  
  5. }  
  6. .left-column {  
  7.     width: $container-width – 300px;  
  8. H2  {  
  9.     font-size: $font-size-normal +4px;  
  10. }  


Similar Articles