What is the difference between LESS and SASS?


SASS and LESS are two popular CSS preprocessors that offer additional features to enhance the efficiency of CSS coding. While both of these preprocessors are quite similar, there are a few differences that set them apart from each other.

In this tutorial, we will explore the differences between SASS and LESS.

What is SASS?

SASS, also known as Syntactically Awesome Style Sheets, provides features like variables, nesting, and mixins to simplify and streamline the CSS writing process. SASS code is written in .scss or .sass files and compiled into regular CSS files for use on the web.

What is LESS?

On the other hand, LESS provides similar features like variables, mixins, and nesting to simplify and organize CSS code. It also includes functions, operations, and control directives to simplify complex styling tasks. LESS files are written in .less format and can be compiled into regular CSS files using a LESS compiler.

Differences Between LESS & SASS

Here are some of the main differences between LESS and SASS −

Basis of Difference

SASS

LESS

Syntax

Sass uses .scss or .sass syntax. Variables are defined using the dollar sign ($), and mixins using the at sign (@).

LESS uses .less syntax. Variables and mixins are defined using the at sign (@).

Code Execution

Sass is slower in code execution than LESS, mainly due to its more extensive feature set, which can take longer to compile.

LESS is faster in code execution than Sass due to its lightweight nature and fewer features.

Compilation

SASS uses Ruby to compile the code into CSS.

LESS uses JavaScript to compile the code into CSS.

Variables

SASS allows for global variables, which can be defined once and used throughout the entire codebase, making it easier to change multiple styles simultaneously.

LESS also allows for variables, but they are local to the block in which they are defined.

Mixins

SASS supports mixins, reusable blocks of code that can be included in multiple places throughout the codebase, allowing for more efficient and modular coding.

LESS also supports mixins but is less flexible than SASS, as they can only be defined with a fixed number of parameters.

Inheritance

SASS allows for inheritance, creating a hierarchical style structure and reducing the need for redundant code.

SASS allows for inheritance, creating a hierarchical style structure and reducing the need for redundant code.

Performance

SASS is generally faster and more lightweight than LESS, as it compiles code more efficiently and includes fewer built-in functions and features.

LESS can be slower and more resource-intensive than SASS, particularly when compiling large codebases or using complex features.

Community Support and Popularity

SASS has a larger community and a more significant following, making it easier to find solutions and resources.

LESS has a smaller community compared to SASS.

Example: LESS

In this example, we will use LESS to style a simple webpage.

Firstly, an HTML file is created with a header, a main section, and a footer. Next, we will create a LESS file called "style. less" and define some styles for our HTML elements.

Finally, when we open our HTML file in a web browser, LESS will compile our "style. less" file into CSS and apply the styles to our HTML elements.

Users can see in the result that the header has a background color of teal, the text is white, the main section has a padding of 20px, and the footer has a dark background color and a link with the same color as the header.

index.html

<html>
<head>
   <title>My Website</title>
   <link rel="stylesheet/less" type="text/css" href="style.less">
   <style>
      @primary-color: #008080;
      header {
         background-color: @primary-color;
         color: #fff;
         padding: 20px;    
         h1 {
            font-size: 36px;
            margin: 0;
         }
      }
      main {
         padding: 20px; 
         p {
            line-height: 1.5;
         }
      }
      footer {
         background-color: #333;
         color: #fff;
         padding: 20px;    
         p {
            margin: 0;
         }    
         .link {
            color: @primary-color;
            text-decoration: none;        
            &:hover {
               text-decoration: underline;
            }
         }
      }
   </style>
   <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.9.0/less.min.js"></script>
</head>
<body>
   <header>
      <h1> Welcome to My Website </h1>
   </header>
   <main>
      <p> This is the main content of the page .</p>
      <p> Here is another paragraph. </p>
   </main>
   <footer>
      <p> © 2023 My Website </p>
   </footer>
</body>
</html>

Output

Example: SASS

In this example, we will create a simple card component using SASS.

First, we define our variables at the top of the SASS file. Background color and font color variables are defined, which are used throughout the component.

Next, we define our mixin for the box-shadow property. This will allow us to easily apply the same box-shadow to multiple elements in our component.

We then define our card component style using the variables and mixin we created.

Finally, we use the card class in our HTML to apply the styles we defined in our SASS file.

index.html

<html>
   <head>
      <title>SASS Example</title>
      <style>

         // Define variables
         $bg-color: #f6fff2;
         $font-color: #004d9e;
         
         // Define mixin for box-shadow property
         @mixin box-shadow($shadow) {
            -webkit-box-shadow: $shadow;
            -moz-box-shadow: $shadow;
            box-shadow: $shadow;
         }
         
         // Define body style
         body{
            background-color: $bg-color;
            color: $font-color;
            font-family: 'Roboto', sans-serif;
            text-align: center;

         }
         
         // Define card component style
         .card {
            background-color: $bg-color;
            color: $font-color;
            width: 50%;
            margin: 0 auto;
            text-align: center;
            padding: 20px;
            border-radius: 5px;
            
            // Nesting to make the code more readable
            h3 {
               margin-bottom: 10px;
            }
            p {
               font-size: 16px;
            }

            // Use mixin for box-shadow property
            @include box-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
         }
      </style>
   </head>
   <body>
      <header>
         <h3> Example Using SASS </h3>
         <div class = "card">
            <h3> Card Heading </h3>
            <p> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ea, corporis. </p>
         </div>
      </header>     
   </body>
</html>

Output

Conclusion

Both Sass and LESS are powerful tools for CSS development, and their choice will depend on individual needs and preferences. Sass offers a more robust set of features and has a larger community, while LESS has better browser support and is generally faster. Ultimately, preprocessors Sass and LESS simplify the writing and maintaining CSS code for modern web applications.

Updated on: 11-May-2023

388 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements