Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less CSS: Mixins with Variable Number of Arguments

LESS allows parametric mixins, such as:

.transition(@property, @duration){     transition:         @property @duration;     -moz-transition:    @property @duration; /* Firefox 4 */     -webkit-transition: @property @duration; /* Safari and Chrome */     -o-transition:      @property @duration; /* Opera */ } 

However, this doesn't always work with properties such as transitions. If you are trying to have multiple transitions and attempt to call the mixin multiple times, the last mixin overrides all previously defined transitions. That's because the proper CSS3 syntax for defining multiple transitions is:

... {     transition: @property1 @duration1, @property2 @duration2, ...; } 

The only way that I can think of to define multiple transitions as mixins is to overload the mixin:

.transition(@property, @duration){...} .transition(@property, @duration, @prop2, @dur2){...} .transition(@property, @duration, @prop2, @dur2, @prop3, @dur3){...} 

Is there a more robust and concise way of defining the transition mixin to take in a variable number of arguments and construct the appropriate transition CSS?

Context: Sometimes I'd like to transition on multiple properties; for example, a :hover might trigger transitions on background color, box-shadow, text-color, etc...

like image 884
Tuanderful Avatar asked Jul 10 '12 18:07

Tuanderful


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.

Can you use CSS variables in LESS?

As we know, less function can't work with css variables.

What is the difference between mixin and variable?

I have been doing some googling and I currently understand the difference being that a variable stores a single line of information whereas, a mixin stores multiple lines of variables.

How do you represent a variable in LESS?

Defining a variable: Variables in Less are represented with an at (@) sign. We can assign a value using a colon (:).


1 Answers

See my answer here: Multiple properties are getting treated as separate arguments in mixins

Summary: use this mixin for variable number of arguments:

.transition (@value1,@value2:X,...) {     @value: ~`"@{arguments}".replace(/[\[\]]|\,\sX/g, '')`;      -webkit-transition: @value;     -moz-transition: @value;     -ms-transition: @value;     -o-transition: @value;     transition: @value; } 
like image 174
David Rettenbacher Avatar answered Sep 21 '22 09:09

David Rettenbacher