LESS - Selectors in Mixins


Advertisements

Description

The mixins can contain not just properties but they can contain selectors too.

Example

The following example demonstrates the use of mixin selectors in the LESS file −

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Selectors in Mixins</title>
   </head>

   <body>
      <h2>Welcome to Tutorialspoint</h2>
      <a href="http://www.tutorialspoint.com/">Tutorialspoint</a>
   </body>
</html>

Next, create the style.less file.

style.less

.mixin() {
   &:hover {
      background: #FFC0CB;
   }
}

a {
   .mixin();
}

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

a:hover {
   background: #FFC0CB;
}

Output

Follow these steps to see how the above code works −

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

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

LESS Mixin Selectors
less_mixins.htm
Advertisements