By Samuel Oloruntoba
This tutorial is out of date and no longer maintained.
Sass is incredible. Whether using it as Sass or SCSS, it improves productivity and makes complex CSS tasks easy. Sass is easy to debug and lets us do more with CSS.
Today’s article is about Sass Mixins, it answers the question of What? Why? and How to use a Mixin?
If you are new to Sass, you check out this Getting Started with Sass.
A Mixin is a block of code that lets us group CSS declarations we may reuse throughout our site.
Take, for example, displaying an HTML element as a Flex element.
There are many elements we want displayed flex, and typing this declaration above over and over gets boring pretty fast. This is where Sass Mixins come in.
Creating a Mixin is very simple, all we have to do is use @mixin
command followed by a space and our Mixin name, then we open and close our curly brackets. Something like this.
Now we can add our flex declaration and use the Mixin anywhere in our code.
Now that we know how to declare Mixins, we can now learn how to use them in our SCSS code.
To use a Mixin, we simply use @include
followed by the name of the Mixin and a semicolon.
After compiling this SCSS code into CSS, our CSS file should look like this.
Mixins can also take in arguments to make the output more dynamic. For example, let’s assume we are building a grid system, and we can choose whether to use flexbox for our layout or floats.
We can create a Mixin, pass an argument to tell it to alternate between flex or floats.
To pass arguments to a Mixin, we simply do this.
Now, when we call the grid
Mixin, we must pass a truthy argument to the Mixin. Just as you’d expect, pass an argument to an invoked Mixin like this.
To let a Mixin receive multiple arguments, we comma-separate the arguments like this.
Functions in programming languages (SASS included) allow default arguments, it only makes sense for mixins too.
The syntax for passing a default argument to a Mixin looks like this.
We can pass as many variables as we want. But, any variable that has a default/optional argument needs to be at the end of the argument list.
Meaning, you can’t do this.
SCSS will throw an error that states Required argument $max-width must come before any optional arguments.
.
Note: Default arguments can be variables, keywords, etc.
In CSS, we have properties like padding and margin that can take anywhere from one to four values for their property arguments.
Like this.
As you know all values above are valid.
In SCSS we also have lists. The padding values above are treated as lists in SCSS. But there is a way for us to tell Mixins to treat these values as variables instead of a list.
To illustrate, let’s create a padding
Mixin.
If we use this Mixin @include padding(2px 4px 6px);
our result will be.
To treat this variable as a list, we add triple dots ...
after the variable name like this.
Now $values
do not get treated as a list but rather a normal variable.
Now we get this.
Another function of the ...
is that it is used to spread arguments.
Sounds weird, an example would better illustrate.
The first thing we did is define two variables $style1
and $style2
, a list and map.
Then we define a box
Mixin that takes 3 arguments. Then we call the Mixin twice, both times using the ...
(spread operator) to pass a list as the only argument in the first instance, and in the second instance, we passed the map.
For the list, since we have 3 list items and the Mixin has 3 arguments, each argument is passed in respectively.
If the list has a length less than 3 it throws an error, otherwise, it takes only the first 3 list items. Meaning our list can be as 10 items and only the first 3 items will is passed to the list.
The map is also treated the same way as the list, the only exception is that the map is treated as keyword arguments (meaning the map key values have to match the variable name).
The above snippet compiles into this.
Note: Any variable argument cannot be placed in front of a regular argument. Same as default/optional arguments.
@content
allows for Mixin extension. We can pass a block to Mixins with the use of @content
.
In short, @content
makes this possible.
To make allow Mixins to receive a block, we simply do this.
And now, our grid Mixin can take in extra CSS. An example use case of this is media queries. Take this snippet for example.
Instead of having to type the media query every time, this feels like a much more stable way to handle media queries.
This is just one example of @content
in action, there are many open-source projects that use it, and I’m sure you have quite a few ideas of your own.
Mixins are obviously very useful and speed up your workflow, and there is a lot we can do with them.
Here is an example of popular open-source Mixins.
nth-child
selectors manageable.You can share your favorite Mixins in the comment.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!