LESS - Extend

Extend is a LESS pseudo class which extends other selector styles in one selector by using :extend selector.
The following example demonstrates the use of extend in the LESS file −
<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "style">
         <h2>Welcome to TutorialsPoint</h2>
         <p>Hello!!!!!</p>
      </div>
   </body>
</html>
Next, create the style.less file.
h2 {
   &:extend(.style);
   font-style: italic;
}

.style {
   background: green;
}
You can compile the extend.less file to extend.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 −
h2 {
   font-style: italic;
}

.style,
h2 {
   background: blue;
}
Follow these steps to see how the above code works −
Save the above html code in the extend_syntax.htm file.
Save the above html code in the extend_syntax.htm file.
Open this HTML file in a browser, the following output will get displayed.
Open this HTML file in a browser, the following output will get displayed.
Extend is placed into ruleset or attached to a selector. It is similar to a pseudo class containing one or more classes, which are separated by comma. Using the optional keyword all, each selector can be followed.
The following example demonstrates the use of extend syntax in the LESS file −
<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "style">
         <h2>Welcome to TutorialsPoint</h2>
         
         <div class = "container">
            <p>Hello!!!!!</p>
         </div>
      
      </div>
   </body>
</html>
Now create the style.less file.
.style:extend(.container, .img) {
   background: #BF70A5;
}

.container {
   font-style: italic;
}

.img {
   font-size: 30px;
}
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 {
   background: #BF70A5;
}

.container,
.style {
   font-style: italic;
}

.img,
.style {
   font-size: 30px;
}
Follow these steps to see how the above code works −
Save the above html code in the extend_syntax.htm file.
Save the above html code in the extend_syntax.htm file.
Open this HTML file in a browser, the following output will get displayed.
Open this HTML file in a browser, the following output will get displayed.
The following table lists all the types of extend syntax which you can use in LESS −
Extend is connected to a selector which looks similar to a pseudo class with selector as parameter.
The &:extend(selector) syntax can be put inside the body of ruleset.
Nested selectors are matched using the extend selector.
By default, extend looks for the exact match between the selectors.
The form of nth expression is important in extend otherwise, it treats selector as different.
When the keyword all is identified at last in the extend argument then LESS matches that selector as part of another selector.
The extend can be connected to interpolated selector.
Extend matches the selector only that is present inside the same media declaration.
It cannot detect the duplication of selectors.
Following are the types of Use Cases for Extend
Classic use case is used to avoid adding the base class in LESS.
Extend is used to move the selector as far as the properties you want to use; this helps in reducing the css generated code.
Using extend we can combine the same styles of a particular selectors into other selector.

    20 Lectures 
     1 hours 

 Anadi Sharma

    44 Lectures 
     7.5 hours 

 Eduonix Learning Solutions

    17 Lectures 
     2 hours 

 Zach Miller

    23 Lectures 
     1.5 hours 

 Zach Miller

    34 Lectures 
     4 hours 

 Syed Raza

    31 Lectures 
     3 hours 

 Harshit Srivastava
 Print
 Add Notes
 Bookmark this page