1. Web Design
  2. LESS

A Deeper Look at LESS Mixins

This post is part of a series called LESS: Beyond the Basics.
A Few Things You Might Not Know About LESS
Creating Color Schemes with LESS Color Functions

Our discussion about LESS continues–this time we’re going to look into LESS mixins. With mixins we can define a group of style declarations once and then reuse them throughout the stylesheet. It helps keep our code DRY as well as producing CSS in a more efficient way, particularly when dealing with complex CSS3 syntax. LESS provides different types of mixins with their very own purpose; they can be straight-forward mixins, or they can work as a (sort of) function. Nonetheless, before we dig deep down into the subject, let’s begin with the basics of how a LESS mixin works, shall we?

Tutorial thumbnail created by Paul Slowinsky from the Noun Project.

The Mixins

LESS always tries to keep its syntax and features so that it closely mimics CSS. Hence a mixin in LESS is declared in the same way as we declare a CSS style rule using a class or an ID selector. Both of the following examples are valid LESS mixins:

Then we can include these mixins into another style rule, for instance:

When compiled, you will have the style declarations from both mixins output within the.header style rule.

Nested Mixins

As you’ve seen above, LESS mixins and (class and ID) selectors appear the same; a LESS mixin is not defined with a special directive like the $ which you’ll find in Sass. LESS gives more flexibility in terms of creating and applying mixins, and it allows us to reuse a nested selector as a mixin as well. Bootstrap uses this approach to construct its gradient mixins

Now, let’s take our previous .border and extend it with a couple of nested selectors, this way:

Assuming you would like to apply the border bottom style you would first need to use the .border, then follow it with the .bottom selector. You can use the nested selector using a > combinator (the same way as we do to select direct child elements in CSS) a space combinator, or compounding the two selectors.

All the above methods will result in the same output:

Parametric Mixins

LESS mixins can also be extended into sort of functions by which they accept one or more parameters, hence being called Parametric Mixins. Through these parameters you are able to customize the mixin output in accordance with what’s passed through the parameters.

The above code snippet shows our previous .border mixin has now turned into a Parametric Mixin with three parameters. Now we are able to customize the border property values, namely the border-width, the style, and the color, for instance:

The output from the above example would be:

Parameter Defaults

Please keep in mind that you would always have to set the values of the parameters explicitly when using a .border mixin like the one shown above. If you don’t LESS will throw an error when you try to compile it.

This error occurred because LESS expected the value of the parameters to be set. We can suppress this error by giving the parameter a default value, as follows:

Now, if the parameter’s values are left empty LESS will fall back to the default values.


Additionally, LESS has a special variable named @arguments which will pass the default values all together. This is a useful shortcut that makes our code looks neater, particularly when dealing with multiple parameters and lengthy values. Take our previous example: we can replace these lengthy variables @border-width @border-style @border-color to simply@arguments.

Mixins Guard

In a nutshell, Mixins Guard is the LESS answer to conditional statement. For many entry users however it may take sometime to grasp and get used to the concept of Mixin Guards, since LESS retains the Mixin form to perform conditional statement and uses the when keyword instead of using the if keyword like one that in many popular programming languages.

So, let's take a very simple example:

The first to the third line of the above code snippet is our Mixin Guard which says: when the.border parameter, @text-color, is set to "red" it should produce border: 1px solid #000;. We use the .border() Mixin into the rule set that follows with the @text-colorincluded as the parameter. The @text-color value is set to "red", hence when compiled the style declaration from the Mixin should be output as the condition is met.

Sass uses != operator to negate the condition. In LESS, you can do so with the when notkeyword, like so:

This time the style declaration of .border Mixin will not be generated given the same example as above.

Generating Default Output of Mixin Guard

Previously, we created a Mixin Guard with single evaluation. This Mixin Guard will not generate any output, unless the evaluation returns true. But, what if you still want to generate an output when the inceptive Mixin Guard condition is not met; you want to generate a default output.

To do this, you will need to create a new Mixin right after the first Mixin Guard.

The evaluation goes this way: If the value passed on the @a variable in the initial Mixin is not "red" it will fall to the next Mixin which will produce border: 1px solid #f3f3f3;. In the other language, say, JavaScript, this would equate to some the following code snippet using the if and else keyword.

When we compile it, we would have the border color to be #f3f3f3 since the variable passed is "red".

Mixins as a Function

What makes this behavior different from parametric mixins? This type of mixin will return output that is reusable within the style rule where it is included, rather than taking values through parameters. Before we immediately jump on the subject, let’s take a look at how a similar idea is done in JavaScript.

Here we have two JavaScript functions: color() and header(). When executed the color()function will produce the value from the color variable. We then include the color()function into the header() and store the output in a new variable, bgColor, which we are now able to reuse in the header() function scope. 

In LESS, we are also able to do this, limiting the variables’ and mixins’ scope where they can be reused.

The above code snippet resembles our JavaScript precedent. We created a .color() function and included it in the .header() that follows, which is why we are able to use the @color variable for our background-color. The code will give us the following when compiled:

Mixins as functions can also contain mixins or parametric mixins, for instance:

...which will result in the following:

Final Thought

LESS provides different types of Mixins which allow us to produce CSS smartly. LESS is rather unique in this regard; it adopts programming traits such as a function, whilst retaining the form of CSS syntax. Hopefully this tutorial has helped you grasp how they work.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.