Sass @mixin and @include
The @mixin directive allows us to define a style that can be reused throughout the stylesheet.
The @include directive can incorporate the mixin into the document.
Defining a Mixin
The following example creates a mixin named "important-text":
Sass Code:
@mixin important-text {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
}
Note: The hyphen - and the underscore _ are identical in Sass, meaning @mixin important-text { } and @mixin important_text { } are the same mixin.
Using a Mixin
The @include directive can be used to include a mixin:
Sass @include Mixin Syntax:
selector {
@include mixin-name;
}
Thus, including the important-text mixin looks like this:
Example
.danger {
@include important-text;
background-color: green;
}
Converting the above code to CSS yields:
CSS Code:
.danger {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
background-color: green;
}
Mixins can also include other mixins, as shown below:
Example
@mixin special-text {
@include important-text;
@include link;
@include special-border;
}
Passing Variables to Mixins
Mixins can accept parameters.
We can pass variables to mixins.
Define a mixin that can receive parameters:
Example
/* Mixin receiving two parameters */
@mixin bordered($color, $width) {
border: $width solid $color;
}
.myArticle {
@include bordered(blue, 1px); // Call mixin with two parameters
}
.myNotes {
@include bordered(red, 2px); // Call mixin with two parameters
}
The mixin parameters in the above example are for setting border properties (color and width).
Converting the above code to CSS yields:
CSS Code:
.myArticle {
border: 1px solid blue;
}
.myNotes {
border: 2px solid red;
}
Mixin parameters can also have default values, with the following syntax:
Example
@mixin bordered($color: blue, $width: 1px) {
border: $width solid $color;
}
When including the mixin, you only need to pass the variable names and their values:
Example
@mixin sexy-border($color, $width: 1in) {
border: {
color: $color;
width: $width;
style: dashed;
}
}
p { @include sexy-border(blue); }
h1 { @include sexy-border(blue, 2in); }
Converting the above code to CSS yields:
CSS Code:
p {
border-color: blue;
border-width: 1in;
border-style: dashed;
}
h1 {
border-color: blue;
border-width: 2in;
border-style: dashed;
}
Variable Arguments
Sometimes, it's not clear how many arguments a mixin or a function will use. In such cases, we can use ...
to set variable arguments.
For example, a mixin for creating box-shadow can take any number of box-shadows as parameters.
Example
@mixin box-shadow($shadows...) {
-moz-box-shadow: $shadows;
-webkit-box-shadow: $shadows;
box-shadow: $shadows;
}
.shadows {
@include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
Converting the above code to CSS yields:
CSS Code:
.shadows {
-moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
-webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}
@include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
Convert the above code to CSS code as follows:
CSS Code:
.shadows {
-moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
-webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}
Using Prefix Mixins
Using prefix mixins is also very convenient, as shown in the following example:
Example
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.myBox {
@include transform(rotate(20deg));
}
Convert the above code to CSS code as follows:
CSS Code:
.myBox {
-webkit-transform: rotate(20deg);
-ms-transform: rotate(20deg);
transform: rotate(20deg);
}