Mixins allow embedding all the properties of a class into another class by including the class name as one of its properties, thus behaving as a sort of constantor variable. They can also behave like functions, and take arguments. CSS does not support Mixins. Any repeated code must be repeated in each location. Mixins allow for more efficient and clean code repetitions, as well as easier alteration of code.
.rounded-corners (@radius: 5px) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; } #header { .rounded-corners; } #footer { .rounded-corners(10px); }
The above code in Less would compile to the following CSS code:
#header { -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; } #footer { -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; }
Less has a special type of ruleset called parametric mixins which can be mixed in like classes, but accepts parameters.