Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptLess.js Extend Selector Interpolation with Extend

Less.js Extend Selector Interpolation with Extend

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 improves CSS and gets compiled into regular CSS syntax so that the web browser can use it. This also provides functionalities like variables, functions, mixins, and operations that enable us to build dynamic CSS.

Extend: LESS Extend is a pseudo-class that helps to merge different selectors, based on putting it with one that matches according to what it is referenced. We can use the LESS extend feature using the: extend keyword

 Selector Interpolation with Extend:

  • Selector Interpolation with Extend. It is used to connect to the interpolated selector.
  • Extend is not able to match selectors with variables. If the selector contains a variable, extend will ignore it.

 

Syntax:

@{variable}:extend(.style) {}
@variable: .selector;

Example 1: The following examples demonstrates the use of Less.js Extend Selector Interpolation with Extend in Less file.

HTML




<!doctype html>
<head>
    <link rel="stylesheet" href="style.css" 
          type="text/css" />
</head>
  
<body>
    <div><br><br>
        <h1><b>neveropen</b></h1>
        <h2 class="style"><b>
          Learning Selector Interpolation with Extend</b></h2>        
    </div>
</body>
</html>


style. less: Create the less file.

CSS




@color: black;
@bg: green;
.style {
    color: @color;
    font-family: "Comic Sans MS";
    text-align: center;
}
h1 {
    color: @bg;
    text-align: center;
}
  
@{variable}:extend(.style) {
}
@variable: .selector;


Now, to compile the above LESS code to CSS code, run the following command:

lessc style.less style.css

The compiled CSS file comes to be: 

style.css

CSS




.style {
    color: black;
    font-family: "Comic Sans MS";
    text-align: center;
}
h1 {
    color: green;
    text-align: center;
}


Output:

 

Example 2: The following example demonstrates the use of Less.js Extend Selector Interpolation with Extend in Less file.

HTML




<!doctype html>
    <head>
      <link rel="stylesheet" href="style.css" 
            type="text/css" />
    </head>
    <body>
        <div><br><br>
            <h1><b>neveropen</b></h1>
            <h2 class="Library">
               <b>Learning Selector Interpolation with Extend</b></h2>
            <br><br><br>        
        </div>
    </body>
</html>


style. less:

CSS




@variable: .Library;
@{variable} {
    color: white;
    text-align: center;
}
h1 {
    color: green;
    text-align: center;
}
div {
    background-color: black;
}
.Library-class:extend(.Library) {
}


Now, to compile the above LESS code to CSS code, run the following command:

lessc style.less style.css

The compiled CSS file comes to be: 

style.css

CSS




.Library,
.Library-class {
    color: white;
    text-align: center;
}
h1 {
    color: green;
    text-align: center;
}
div {
    background-color: black;
}


Output:

 

Reference: https://lesscss.org/features/#extend-feature-selector-interpolation-with-extend

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments