LESS - Conditional Mixins



Description

You can use the default function to match mixin with other mixing matches and create conditional mixins which look like else or default statements.

Example

The following example demonstrates the use of conditional mixins in the LESS file −

<!doctype html>
   <head>
      <title>Conditional Mixins</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Conditional Mixins</h2>
      <p class = "myclass">LESS enables customizable, manageable and reusable style sheet for web site.</p>
   </body>
</html>

Next, create the style.less file.

style.less

.mixin (@a) when (@a > 22px) {
   color:blue;
}

.mixin (@a) when (@a <= 20px) {
   color:red;
}

.mixin (@a) {
   font-size: @a;
}

.myclass { .mixin(20px) }

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

.myclass {
   color: red;
   font-size: 20px;
}

Output

Follow these steps to see how the above code works −

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

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

Mixin Guards
less_mixin_guards.htm
Advertisements