Why SASS is considered better than LESS?


SASS and LESS are CSS preprocessor languages that help developers write CSS more quickly, reduce code duplication, and create modular stylesheets. Instead of writing repetitive code for each property, Sass and Less allow us to define variables for values like colors, font sizes, and other properties, which can be easily reused throughout our stylesheets. This makes updating styles across a project easier and maintains consistency throughout our design.

However, Sass is considered better than Less for several reasons. Let’s understand the features and functionality of both of them. So that you can get more clearance about these preprocessor languages and why Sass is the preferred option for many developers and development teams.

Why SASS is the Better Choice

Sass is better than Less for more complicated projects because it has more features and capabilities.

Variables

Both Sass and Less allow the use of variables. In Sass, we use the $ symbol to declare a variable, while in Less, we use the @ symbol.

Here’s an example

SASS

$primary-color: #007bff;
.nav {
   background-color: $primary-color;
}

LESS

@primary-color: #007bff;
.nav {
   background-color: @primary-color;
}

Functions

Sass provides a wider range of built-in functions and allows for the creation of custom functions, whereas Less only includes a limited set of basic functions.

SASS

$button-bg: #007bff;
$button-color: contrast($button-bg);
button {
   background-color: $button-bg;
   color: $button-color;
}

LESS

@button-bg: #007bff;
@button-color: lighten(@button-bg, 20%);
button {
   background-color: @button-bg;
   color: @button-color;
}

Inheritance

Sass and Less use inheritance to reduce code duplication by sharing styles between multiple CSS classes. However, Sass supports more advanced inheritance features such as placeholders, which allow for more modular and efficient styles, and less only supports basic inheritance through mixins.

SASS

%message-shared {
   border: 1px solid #ccc;
   padding: 10px;
   color: #333;
}
.message {
   @extend %message-shared;
}
.success {
   @extend %message-shared;
   border-color: green;
}

LESS

.message-shared { 
   border: 1px solid #ccc; 
   padding: 10px; 
   color: #333; 
} 
.message { 
   .message-shared; 
} 
.success { 
   .message-shared; 
   border-color: green; 
}

Community and Resources

Sass has been around longer than Less and has a larger user base. This means that there are more resources available for learning and using Sass.

The Sass community has developed many libraries and frameworks, such as

  • Bourbon

  • Compass, and

  • Susy,

which can help speed up development and improve code quality. Additionally, many online tutorials, documentation, and support forums are available for Sass. These resources can be helpful for developers to ask questions and get help. Overall, Sass has a larger and more active community, which makes it a more attractive option for developers.

Compatibility

Additionally, Sass is compatible with Ruby and Node.js, while Less only works with JavaScript. Sass can be used with various development environments and frameworks, making it a more flexible option. For example, popular frameworks such as Bootstrap, Foundation, and Materialize all support, Sass.

Example

In the example below, we illustrate how Sass's advanced control structures and inheritance can be used to simplify and streamline CSS development, which is not possible in Less:

In SASS, we define a common set of button styles using the %button-style placeholder selector. The @extend directive is used to inherit these styles in the .button-primary and .button-secondary classes.

We then use LESS, define the .button-style class with the common styles, and then include it using nested selectors in the .button-primary and .button-secondary classes..

In the output, users can see that the LESS approach results in more verbose and repetitive code, and makes it harder to maintain the styles if changes are required in the future.

SASS

%button-style {
   display: inline-block;
   padding: 10px 20px;
   font-size: 16px;
   text-align: center;
   color: #ffffff;
   background-color: #007bff;
   border-radius: 5px;
} 
.button-primary {
   @extend %button-style;
} 
.button-secondary {
   @extend %button-style;
   background-color: #6c757d;
}

LESS

.button-style {
   display: inline-block;
   padding: 10px 20px;
   font-size: 16px;
   text-align: center;
   color: #ffffff;
   background-color: #007bff;
   border-radius: 5px;
} 
.button-primary {
   .button-style;
} 
.button-secondary {
   .button-style;
   background-color: #6c757d;
}

Output

.button-primary, .button-secondary {
   display: inline-block;
   padding: 10px 20px;
   font-size: 16px;
   text-align: center;
   color: #ffffff;
   background-color: #007bff;
   border-radius: 5px;
} 
.button-secondary {
   background-color: #6c757d;
}

Example

In this example, we explained how Sass and Less set font sizes for different breakpoints. Sass uses maps and a loop to generate the CSS, while Less uses individual variables and media queries. Sass's approach is more concise and efficient, allowing for easier management of multiple breakpoints and font sizes. The Less code is longer and less dynamic, requiring more manual adjustment for changes.

Overall, Sass's ability to handle complex styling situations easily and efficiently is one reason it is considered superior to Less.

SASS

$breakpoints: (
   sm: 576px,
   md: 768px,
   lg: 992px,
   xl: 1200px
);
 
$font-sizes: (
   sm: 14px,
   md: 16px,
   lg: 18px,
   xl: 20px
);
 
@each $bp, $width in $breakpoints {
   @media (min-width: $width) {
      font-size: map-get($font-sizes, $bp);
   }
}

LESS

@breakpoint-sm: 576px;
@breakpoint-md: 768px;
@breakpoint-lg: 992px;
@breakpoint-xl: 1200px;
 
@font-size-sm: 14px;
@font-size-md: 16px;
@font-size-lg: 18px;
@font-size-xl: 20px; 
.my-element {
   @media (min-width: @breakpoint-sm) {
      font-size: @font-size-sm;
   } 
   @media (min-width: @breakpoint-md) {
      font-size: @font-size-md;
   }
   @media (min-width: @breakpoint-lg) {
      font-size: @font-size-lg;
   } 
   @media (min-width: @breakpoint-xl) {
      font-size: @font-size-xl;
   }
}

Output

@media (min-width: 576px) {
  .my-element {
    font-size: 14px;
  }
}
@media (min-width: 768px) {
  .my-element {
    font-size: 16px;
  }
}
@media (min-width: 992px) {
  .my-element {
    font-size: 18px;
  }
}
@media (min-width: 1200px) {
  .my-element {
    font-size: 20px;
  }
}

Conclusion

Users learned about the advantages that make Sass the better choice for more complex projects. Users also learned that Sass also has a larger and more active community, which provides more resources, documentation, and support for developers.

Finally, Sass is more widely used and supported by various development environments, making it a more versatile choice for complex CSS development. However, Less can still be a useful tool for basic CSS development. Ultimately, the choice between Sass and Less depends on the specific needs and requirements of the project and development team.

Updated on: 11-May-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements