Understanding CSS Mixins

What is CSS Mixins?

Mixins allow document authors to define patterns of property value pairs, which can then be reused in other rulesets.

The mixin name is a class selector that identifies the mixin being declared. The @mixin keyword must be followed by the mixin name and a declaration block.

Mixins Examples

Take a look at those examples and you can see it clearly

@mixin .noborder {   
overflow: hidden;   
_overflow: visible;   
border: 0px solid #fff; 
}

Here, we defined a mixin called .nobody with some value pairs those can be reused anywhere.

Media Independent Mixins

Add media query AFTER the class name to specify the media query. Look at this example:

@mixin .noborder @media print{
  border: 1px solid #111;
}

This mixin will applied to @media print only.

Include/Add a mixins

You can use include command to use a mixins

.tablex {
  include: .noborder;
}

Include multiple mixins in one command

.tablex {
  include: .noborder .mixin2 .mixin3;
}