LESS - Mixins with Multiple Parameters


Advertisements

Description

Parameters can be separated using commas or semicolon. Using the comma symbol, you can interpret it as mixin parameters separator or css list separator. If you use semicolon inside mixin, then it separates the arguments by semicolons and CSS lists will be having all the commas.

It includes some points on semicolons and commas as listed below −

  • If you have two arguments, then the arguments will contain commas separated list. For instance, .class1(1, 2, 3; sometext, other thing).

  • If there are three arguments, the arguments will include only numbers such as .class1(1, 2, 3).

  • You can use dummy semicolon with comma separated list like .class1(1, 2, 3;).

  • There is comma separated default value. For instance .class1(@color: gray, green;)

Syntax

.mixin_name(@var_name1; @var_name2:some) {
   //code here
}

Example

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

<!doctype html>
   <head>
      <title>Mixin Multiple Parameters</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Mixin Multiple Parameters</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(@color) {
   color: @color;
}

.mixin(@color; @padding: 2) {
   color: @color;
   padding: @padding;
}

.myclass {
   .mixin(#FE9A2E);
}

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: #FE9A2E;
   padding: 2;
}

Output

Follow these steps to see how the above code works −

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

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

Mixin Multiple Parameters
less_parametric_mixins.htm
Advertisements