SASS is a super-set of CSS, that means, any existing CSS that you already wrote will just work. What we usually do, is paste any legacy CSS (if any) to our SASS file and take it from there. I’ve posted few examples of the syntax below. In my next post, I will focus on the real-world usage of SASS.

The useful stuff – Nested Syntax

If you like tidy CSS syntax, you’ll love this. Not my most favourite, but still worth mentioning. So something like this

ul
    {
        margin: 0;
        li 
        {
            float: left;
        }
    }

will be transferred into

ul { margin: 0; }
    ul li {float: left;}

Cool Stuff – Variables

SASS lets you create variables and re-use them throughout. These may be plain your colours that you re-use or size values. Using SASS syntax, you can perform various calculations on them if need be.

Declaring SASS variables

$primary-color: #ccc;
    $secondary-color: #ebebeb;
    $grid-height: 20;
     
    a { color: $primary-color;}
    a:hover { color: $secondary-color;}
    div.test { height: $grid-height + 10 + px;}

results into

a {
      color: #cccccc;
    }
    a:hover {
      color: #ebebeb;
    }
    div.test {
      height: 30px;
    }

Really Cool Stuff- Mixins and Inheritance

One of the key features of sass is the ability to declare mixins. These are basically functions that you can declare to run common tasks. For example, we can declare a function that handles background images for us

@mixin background($url, $color:transparent, $repeat:no-repeat, $position:0 0) {
        background: $color url($url) $repeat $position;
    }
     
    div {
        @include background('/some/url');
    }
     
    div.second {
        @include background('/some/url', '#ebebeb');
    }

which will transform into

div {
      background: transparent url("/some/url") no-repeat 0 0;
    }
    div.second {
      background: "#ebebeb" url("/some/url") no-repeat 0 0;
    }

The Really, Really Cool Stuff – Compass Libraries

Now since we use compass, we can also tap into some existing methods and libraries inside compass. The compass core includes reset, layout and typography helpers, CSS helpers and CSS3 libs and utilities.

Check them out at http://compass-style.org/reference/compass/

Uber Cool Stuff – Maths, Operations, Loops and conditions

Bring it all together, and you can go wild … quite wild … something like this

$gutterWidth: 20;
    $columns: 12
    $widthForColumns: $totalWidth - ($columns * $gutterWidth);
    $columnWidth: floor($widthForColumns/$columns);
     
    @for $i from 1 through $columns {
      .container_12 .grid_#{$i} { width:(($columnWidth*$i) + (($gutterWidth*$i)-$gutterWidth))+px; }
    }

Related Articles

SASS with Visual Studio Part 1 Introduction
SASS with Visual Studio Part 2 Features and Syntax
SASS with Visual Studio Part 3 Real World
SASS/SCSS and Compass Part 4 Spriting

Comments