LESS - Selectors



Description

The selector can reference any variable and it is built during the compile time. The variable name must be placed inside the curly braces( { } ) prefixed with the @ symbol.

Example

The following example demonstrates the use of selector in the LESS file −

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>LESS selectors</title>
   </head>
	
   <body>
      <h2>Welcome to Tutorialspoint</h2>
         
      <div class = "div1">
         <p>LESS is a CSS pre-processor that enables customizable, 
            manageable and reusable style sheet for web site.</p>
      </div>
      
      <div class = "div2">
         <p>LESS is a dynamic style sheet language that extends the capability of CSS. 
            LESS is also cross browser friendly.</p>
      </div>
   </body>
	
</html>

Now create the style.less file.

style.less

@selector: h2;

@{selector} {
   background: #2ECCFA;
}

You can compile the style.less 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

h2 {
   background: #2ECCFA;
}

Output

Follow these steps to see how the above code works −

  • Save the above html code in the less_variables_interpolation_selectors.html file.

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

LESS Selectors
less_variables.htm
Advertisements