Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less mixin with optional parameters

I have a Less mixin defined as:

.fontStyle(@family, @size, @weight: normal, @style: normal, @color: #ffffff, @letter-spacing: normal) {   font-family: @family;   font-size: @size;   color: @color;   font-weight: @weight;   font-style: @style;  letter-spacing: @letter-spacing; } 

How can I define usage like:

.fontStyle('NimbusSansNovCon-Reg', 12px, , , , 0.1em); 

I.E. use the defaults for @weight, @style, @color

like image 229
Justin Avatar asked Jun 14 '13 19:06

Justin


People also ask

What can be passed to a less mixin?

Mixins are a group of CSS properties that allow you to use properties of one class for another class and includes class name as its properties. In LESS, you can declare a mixin in the same way as CSS style using class or id selector. It can store multiple values and can be reused in the code whenever necessary.

Are multiple parameters separated by semicolons?

Multiple parameters are separated by semicolons. Argument names must match parameter names. The order of the arguments passed to a procedure need to correspond to the order of the parameters declared in the procedure.

Which arguments are used to include arguments in the mixins?

Keyword ArgumentsExplicit keyword argument can be used to include in mixins. The arguments, which are named can be passed in any order and the default values of argument can be omitted.

How to separate the @parameters in a mixin?

Parameters can be separated using commas or semicolon. Mixins provide parameter values instead of positions by using their names. When a mixin is called, the @arguments include all the passed arguments. Mixin takes variable number of arguments by using .....

How to make an argument of mixin optional?

We can make an argument of mixin optional by defining its default value that argument isn’t passed by @include. Passing null or 0 or no argument passed by @include of mixin led to Optional Arguments. Example 1: Below example illustrates above approach.

What is parametric mixin in less?

Parametric mixins use one or more parameters that extend functionality of LESS by taking arguments and its properties to customize the mixin output when mixed into another block. Here we are using the parametric mixin as .border with three parameters - width, style and color.

What happens when @include of mixin passes null or 0 arguments?

Passing null or 0 or no argument passed by @include of mixin led to Optional Arguments. Example 1: Below example illustrates above approach. Example 2: Below example illustrates above approach.


2 Answers

To supply a parameter that far down the string of arguments, you have to also supply the expected variable it is to define. So this:

.fontStyle('NimbusSansNovCon-Reg', 12px, @letter-spacing: 0.1em); 

Produces this (note how color, font-weight, and font-style used the defaults):

font-family: 'NimbusSansNovCon-Reg'; font-size: 12px; color: #ffffff; font-weight: normal; font-style: normal; letter-spacing: 0.1em; 
like image 145
ScottS Avatar answered Sep 19 '22 09:09

ScottS


See the documentation

http://lesscss.org/features/#mixins-parametric-feature-mixins-with-multiple-parameters

Note: you should get in the habit of using semicolons to separate parameters since some css values can contain commas.

like image 40
Simon_Weaver Avatar answered Sep 19 '22 09:09

Simon_Weaver