I'm trying to create a sass mixin for transitions. This is what I have so far.
@mixin transition($var)
-webkit-transition: $var
transition: $var
I want to be able to pass it multiple arguments like this
@include transition(color .5s linear, width .5s linear)
Unfortunately, I get the following error
Syntax error: Mixin transition takes 1 argument but 2 were passed.
Is there a way to do this so it produces the following output in css while still accepting an undefined number of arguments?
-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;
When you call the mixin, call it like this:
With the extra parens. This will key sass into the fact that you want this used as a single argument.
EDIT: See Jeremie Parker's answer above if using Sass 3.2 or later. Real Variable Arguments were added in 3.2: http://chriseppstein.github.io/blog/2012/08/23/sass-3-2-is-released
Compass has a transition mixin that you could take a look at (or you could just use Compass). You can take a better look at it here: http://beta.compass-style.org/reference/compass/css3/transition/.
By the looks of it you can't do an undefined number of mixins as the maintainer of Compass also aids in maintaining Sass and you can see that he has defined a maximum number of 10 separate arguments for his transition mixin.
Variable Arguments
Sometimes it makes sense for a mixin to take an unknown number of arguments. For example, a mixin for creating box shadows might take any number of shadows as arguments. For these situations, Sass supports “variable arguments,” which are arguments at the end of a mixin declaration that take all leftover arguments and package them up as a list. These arguments look just like normal arguments, but are followed by
...
. For example:is compiled to:
From : SASS official Documentation
So you basically just need to change your mixins declaration to look like this :
In case you want multiple arguments and vendor-prefixed, like the fallowing scenario:
Expected
I suggest you this Mixin, I found on Meaningless Writing.
Code