LESS - Reducing CSS Size


Advertisements

Description

Extend is used to move the selector as far as the properties you want to use, which helps in reducing the css generated code.

Example

The following example shows how to reduce the css size in a LESS file −

extend_syntax.htm

<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "cont">
         
         <div class = "style">
            <h2 class = "cont">The largest Tutorials Library on the web.</h2>
            <ul>
               <li>HTML</li>
               <li>SASS</li>
               <li>LESS</li>
            </ul>
         </div>
      
      </div>
   </body>
</html>

Now create the style.less file.

style.less

.style {
   display: inline-block;
   background-color: black;
   color: white;
}

.cont {
   &:extend(.style);
}

.nav {
   &:extend(.style);
}

You can compile the style.less file to style.css by using the following command −

lessc style.less style.css

Execute the above command; it will create the style.css file automatically with the following code −

style.css

.style,
.cont,
.nav {
   display: inline-block;
   background-color: black;
   color: white;
}

Output

Follow these steps to see how the above code works −

  • Save the above html code in the extend_syntax.htm file.

  • Open this HTML file in a browser, the following output will get displayed.

Less Extend
less_extend.htm
Advertisements