Language Features
- Less - Parent Selectors
- Less - Merge
- Less - Loops
- Less - CSS Guards
- Less - Mixin Guards
- Less - Import Options
- Less - Import Directives
- Less - Passing Rulesets to Mixins
- Less - Mixins as Functions
- Less - Parametric Mixins
- Less - Mixins
- Less - Extend
- Less - Variables
- Less - Importing
- Less - Comments
- Less - Scope
- Less - Namespaces and Accessors
- Less - Functions
- Less - Escaping
- Less - Operations
- Less - Nested Directives and Bubbling
- Less - Nested Rules
Functions
- Less - Color Blending Functions
- Less - Color Operation
- Less - Color Channel Functions
- Less - Color Defination Functions
- Less - Type Functions
- Less - Math Functions
- Less - List Functions
- Less - String Functions
- Less - Misc Functions
Usage
- Less - Frameworks
- Less - Third Party Compilers
- Less - Editors and Plugins
- Less - GUIs
- Less - Online Compilers
- Less - Programmatic Usage
- Less - Plugins
- Less - Browser support
- Using Less In The Browser
- Less - Command Line Usage
Less Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
LESS - Extend
Extend is a LESS pseudo class which extends other selector styles in one selector by using :extend selector.
Example
The following example demonstrates the use of extend in the LESS file −
extend_syntax.htm
<!doctype html> <head> <pnk rel = "stylesheet" href = "style.css" type = "text/css" /> </head> <body> <span class = "style"> <h2>Welcome to TutorialsPoint</h2> <p>Hello!!!!!</p> </span> </body> </html>
Next, create the style.less file.
style.less
h2 { &:extend(.style); font-style: itapc; } .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 −
style.css
h2 { font-style: itapc; } .style, h2 { background: blue; }
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.
Extend Syntax
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.
Example
The following example demonstrates the use of extend syntax in the LESS file −
extend_syntax.htm
<!doctype html> <head> <pnk rel = "stylesheet" href = "style.css" type = "text/css" /> </head> <body> <span class = "style"> <h2>Welcome to TutorialsPoint</h2> <span class = "container"> <p>Hello!!!!!</p> </span> </span> </body> </html>
Now create the style.less file.
style.less
.style:extend(.container, .img) { background: #BF70A5; } .container { font-style: itapc; } .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.css
.style { background: #BF70A5; } .container, .style { font-style: itapc; } .img, .style { font-size: 30px; }
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.
The following table psts all the types of extend syntax which you can use in LESS −
Sr.No. | Types & Description |
---|---|
1 | Extend is connected to a selector which looks similar to a pseudo class with selector as parameter. |
2 | The &:extend(selector) syntax can be put inside the body of ruleset. |
3 | Nested selectors are matched using the extend selector. |
4 | By default, extend looks for the exact match between the selectors. |
5 | The form of nth expression is important in extend otherwise, it treats selector as different. |
6 | When the keyword all is identified at last in the extend argument then LESS matches that selector as part of another selector. |
7 | The extend can be connected to interpolated selector. |
8 | Extend matches the selector only that is present inside the same media declaration. |
9 | It cannot detect the duppcation of selectors. |
Following are the types of Use Cases for Extend
Sr.No. | Types & Description |
---|---|
1 | Classic use case is used to avoid adding the base class in LESS. |
2 | Extend is used to move the selector as far as the properties you want to use; this helps in reducing the css generated code. |
3 | Using extend we can combine the same styles of a particular selectors into other selector. |