What is the use of extend in LESS ?
Last Updated :
10 Oct, 2022
LESS is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables and so on which helps to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will learn about the use of the extend feature in LESS.
Extend in LESS helps us to merge different selectors it is put on with. We can use the LESS extend feature using the :extend keyword.
Syntax:
.elem1:extend(.elem1){};
It can also be used inside a class to extend the properties of a class.
&:extend(.elem1);
Example 1: We use the extend feature inside the .btn2 block to extend the feature of the .btn1 class.
style. less
JavaScript
.btn1{
background-color: blue;
border: 2px solid white;
}
.btn2{
color: whitesmoke;
&:extend(.btn1);
}
Syntax: To compile the less file to a CSS file, write the following command:
lessc style.less style.css
After executing the above command, it will create the style.css file automatically with the following code:
Output:
style.css
CSS
.btn1,
.btn2 {
background-color: blue;
border: 2px solid white;
}
.btn2 {
color: whitesmoke;
}
Example 2: In this example, .btn2 is extended with the .btn1 class.
style. less
JavaScript
.btn1{
background-color: blue;
border: 2px solid white;
}
.btn2:extend(.btn1){};
Syntax: To compile the less file to a CSS file, write the following command:
lessc style.less style.css
After executing the above command, it will create the style.css file automatically with the following code:
Output:
style.css
CSS
.btn1,
.btn2 {
background-color: blue;
border: 2px solid white;
}
We can also extend multiple selectors into one selector. This can be done using two different methods. Syntax of both is the method are given below:
Extending Interpolated Selectors: The extend keyword cannot be used to match selectors with variables but it works with interpolated variables. Â
Example 1:
style. less
JavaScript
@variable: .heading2;
@{variable} {
color: blue;
}
.heading1:extend(.heading2) {}
Syntax: To compile the less file to a CSS file, write the following command:
lessc style.less style.css
After executing the above command, it will create the style.css file automatically with the following code:
Output:
style.css
CSS
.heading2{
color: blue;
}
Example 2:
style. less
JavaScript
.heading1 {
color: blue;
}
@{variable}:extend(.heading1) {}
@variable: .heading2;
Syntax: To compile the less file to a CSS file, write the following command:
lessc style.less style.css
After executing the above command, it will create the style.css file automatically with the following code:
Output:
style.css
CSS
.heading1, .heading2 {
color: blue;
}
Extend inside the @media: If we call extend from the inside of @media, it will only match the selectors inside the media and the rest will be left alone. Given below is a code example that shows the following, in which we have to extend a selector inside @media braces. The below example explains this.
Example:
style. less
JavaScript
@media print {
.head1:extend(.head2) {}
.head2 {
color: black;
}
}
.head2 {
color: red;
}
@media screen {
.head2 {
color: blue;
}
}
Syntax: To compile the less file to a CSS file, write the following command:
lessc style.less style.css
After executing the above command, it will create the style.css file automatically with the following code:
Output: Extend only extends those selectors are inside the same @media and ignores everything else.
style.css
CSS
@media print {
.head2,
.head1 {
color: black;
}
}
.head2 {
color: red;
}
@media screen {
.head2 {
color: blue;
}
}
Reference:https://lesscss.org
Similar Reads
What is the use of Escaping in LESS ?
In this article, we will know the concept of Escaping in LESS & how to implement & use it in the LESS code that will generate the corresponding CSS code. By using the CSS preprocessor languages i.e., LESS which helps to reduce the code complexity of writing the CSS. Escaping in LESS, helps u
2 min read
What is the use of Mixins in LESS ?
In this article, we will know what is Mixins in LESS, along with understanding their implementation & usage through the example. A CSS preprocessor is a program that generates the CSS code by using the defined preprocessor ruleset & facilitates reducing the overall complexity of CSS code. LE
5 min read
What is the use of operations in LESS ?
Similar to many programming languages, LESS also provides arithmetic operations like addition (+), subtraction (-), division (/) and multiplication (*). These operators can make our work a lot easier when used properly. It is as simple as doing basic mathematics in the stylesheets and can be applied
1 min read
What is the use of Namespaces and Accessors in Less ?
LESS (stands for Leaner Style Sheets) is a backward-compatible extension for CSS. LESS is a CSS pre-processor that provides you to customize, arrange, and utilize the style sheets for the webpage. Less is a dynamic style sheet language that can be executed on the client-side or server-side. Less com
2 min read
Less.js Extending Nested Selectors
LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a dynamic style sheet language that increases the working power of CSS. LESS is cross-browser compatible. CSS pre-processor is a scripting language that impro
2 min read
What are nested rules in LESS ?
In this article we will learn about nested rules in Less, We can very easily define nested rules in LESS. Nested rules are defined as a set of CSS properties that allow the properties of one class to be used for another class and contain the class name as its property. In LESS, you can use class or
3 min read
Less.js Extend Syntax & Inside Ruleset
LESS.js is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables, and, so on, which help to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will see the basic usa
2 min read
What is a @extend directive in SASS ?
Sass is short for Syntactically Awesome Style Sheet. It is an upgrade to Cascading Style Sheets (CSS) and is one of the popular pre-processors available for writing CSS. The @extend is a keyword in Sass that allows one to share a set of CSS properties from one selector to another. This is useful for
3 min read
@include vs @extend in SASS
@include keyword is used to include the code written in a mixin block. @mixin is used to group css code that has to be reused a no of times. Whereas the @extend is used in SASS to inherit(share) the properties from another css selector. @extend is most useful when the elements are almost same or ide
2 min read
Include v/s Extend in Ruby
Include is used to importing module code. Ruby will throw an error when we try to access the methods of import module with the class directly because it gets imported as a subclass for the superclass. So, the only way is to access it through the instance of the class. Extend is also used to importin
2 min read