Comparing CSS Preprocessors : SASS and LESS

Comparing CSS Preprocessors : SASS and LESS

0 21740
SASS and LESS

Sass and LESS both are CSS Preprocessors. These are two of the most commonly used processors in the industry. CSS processors are very powerful and help you to streamline the development process. Although, both of these share many similarities in syntax, the main difference between them is the way they are processed. LESS is a Javascript library and it is processed at the client side. Whereas Sass runs on Ruby and it is processed at the client side.

Let us try to understand some of the difference between Sass and LESS:

Getting Started:

Let us start from the very first step and that is installation. LESS and Sass both are built upon different platforms . As we have already read above that Sass runs on Ruby while LESS is a Javascript library.

Getting started with Sass:

Sass requires Ruby to work. Ruby is pre-installed in Mac but in the case of Windows you have to first install Ruby before you start working with Sass. Moreover, you have to install Sass through the terminal or the Command Prompt.

Getting Started with LESS:

Less is easy to install as it is build upon Javascript. It is as easy as linking the JavaScript library to your HTML document. There are some well performing GUI applications to help in compiling LESS to CSS and most of them are free which makes it even better.

1.Nesting:

Sass and LESS both allows Nesting. Sass allows you to nest individual properties which take nesting to a new level.

With Sass

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    display: inline-block;
  }
  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

CSS Output

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
nav li {
  display: inline-block;
}
nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
}

With LESS

#header {
  color: black;
  .navigation {
    font-size: 12px;
  }
  .logo {
    width: 300px;
  }
}

CSS Output

#header {
  color: black;
}
#header .navigation {
  font-size: 12px;
}
#header .logo {
  width: 300px;
}

2. Mixins:

Mixins are defined a bit differently in Sass and LESS. We use a @mixin directive in Sass, While it is defined as a class selector in LESS. For Example:

Sass:

@mixin border-radius ($values) {
  border-radius: $values;
}
nav {
  margin: 50px auto 0;
  width: 788px;
  height: 45px;
  @include border-radius(10px);
}

Less:

.border(@radius) {
  border-radius: @radius;
}
nav {
  margin: 50px auto 0;
  width: 788px;
  height: 45px;
  .border(10px);
}

Mixins are used to include properties from one ruleset to another ruleset. This method goes further with Selector Inheritance in Sass. The concept is same, but Sass extends or groups selectors that have the same properties and values using the @extend directive instead of copying the whole properties.

For example:

.circle {
  border: 1px solid #ccc;
  border-radius: 50px;
  overflow: hidden;
}
.avatar {
  @extend .circle;
}

It will result in:

.circle, .avatar {
  border: 1px solid #ccc;
  border-radius: 50px;
  overflow: hidden;
}

It shows that Sass is one step ahead by distinct Mixins and Selectors Inheritance.

3.Conditional Statements:

This is something which is not present in LESS. Sass enables you to use if {} else {} conditional statements and for {} loops as well. It also supports the operators like or, and ,not and <,>,<=,>= , == .

/* Sample Sass "if" statement */
@if lightness($color) >; 30% {
  background-color: #000;
} @else {
  background-color: #fff;
}
/* Sample Sass "for" loop */
@for $i from 1px to 10px {
 .border-#{i} {
    border: $i solid blue;
  }
}

4. Loops:

Less allows you to loop only the numeric values. Whereas Sass empowers you to iterate through any kind of data.

For Example:-

LESS

.looper (@i) when (@i > 0) {
  .image-class-@{i} {
    background: url("../img/@{i}.png") no-repeat;
  }
  .looper(@i – 1);
}
.looper(0);
.looper(3);
//--------------- Outputs: --------------------
//.image-class-3 {
// background: url("../img/3.png") no-repeat;
//}
//.image-class-2 {
// background: url("../img/2.png") no-repeat;
//}
//.image-class-1 {
// background: url("../img/1.png") no-repeat;
//}

SASS

@each $beer in stout, pilsner, lager {
  .#{$beer}-background {
    background: url("../img/beers/#{$beer}.png") no-repeat;
  }
}
// ------------------- Outputs: ---------------------
//.stout-background {
// background: url("../img/beers/stout.png") no-repeat;
//}
//.pilsner-background {
// background: url("../img/beers/pilsner.png") no-repeat;
//}
//.lager-background {
// background: url("../img/beers/lager.png") no-repeat;
//}

It is a clear fact that it is much more helpful to be able to iterate any kind of data.

Numbers:

Numbers and basic arithmetic are supported by Sass and Less both. However, there is a difference in the way how they handle the units.
Sass supports unit-based arithmetic. It supports complex units in any immediate form. Moreover, Sass has the conversion table which enables it to combine any comparable units.

SASS

1cm * 1em => 1 cm * em
2in * 3in => 6 in * in
(1cm / 1em) * 4em => 4cm
2in + 3cm + 2pc => 3.514in
3in / 2in => 1.5

Sass allows you to define your own units and can print out unknown units into your css. Less will not do it for you. It is done by Sass as a form of future proofing against changes in the w3c specification or in case a non-standard unit is introduced by the browser.

LESS

1cm * 1em => Error
2in * 3in => 6in
(1cm / 1em) * 4em => Error
2in + 3cm + 2pc => Error
3in / 2in => 1.5in

Colors:

Sass exposes a long list of color functions. It also provides an array of tools that helps to manipulate the colors.

Accessors:

red($color)
green($color)
blue($color)
hue($color)
saturation($color)
lightness($color)
alpha($color)

Mutators:

lighten($color, $amount)
darken($color, $amount)
saturate($color, $amount)
desaturate($color, $amount)
adjust-hue($color, $amount)
opacify($color, $amount)
transparentize($color, $amount)
mix($color1, $color2[, $amount])
grayscale($color)
compliment($color)

Variables:

With processors, it is possible for you to use the variables. Both Sass and LESS have variables. The difference is Sass define the variable with while LESS define the variable with sign.

For example in LESS

@mainLessColor: #ff0087;
p {color: @mainLessColor;}

And in Sass

$mainSassColor: #ff0087;
p {color: $mainSassColor;}

Both are almost same. From the user point of view, there is some problem with LESS as @ has a meaning in CSS which may create confusion while $ does not have any meaning so it does not create any problem.

Both of these are fantastic tools and can help you to work quickly and efficiently. However, you can not really say that which processor is better as it is the user’s call. If you prefer Ruby then you may opt for Sass, while if you are using PHP and Javascript then LESS may be a good option for you.
It totally depends upon your comfort level and your specific requirements that which one you opt for.

Author Bio: Mary Scott is a WordPress Developer by profession and writer by hobby. She works for Stellen Infotech – WordPress Theme Customization Company providing Web Solutions to global Clients.

NO COMMENTS

Leave a Reply