Sass @mixin and @include
The @mixin directive allows us to define a style that can be reused throughout the stylesheet.
The @include directive can import a mixin into the document.
Define a mixin
A mixin is defined via the @mixin directive. @mixin name { property: value; property: value; ... }
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:** In Sass, the hyphen - and underscore _ are equivalent, 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
;
}
Therefore, the code to include the important-text mixin is as follows:
Example
.danger
{
@include
important-
text
;
background-color
:
green
;
}
Convert the above code into CSS code as shown below:
CSS code:
.danger
{
color
:
red
;
font-size
:
25px
;
font-weight
:
bold
;
border
:
1px
solid
blue
;
background-color
:
green
;
}
A mixin can also include another mixin, as shown below:
Example
@mixin
special-
text
{
@include
important-
text
;
@include
link
;
@include
special-
border
;
}
Passing variables to a mixin
A mixin can accept parameters.
We can pass variables to a mixin.
Define a mixin that can accept parameters:
Example
/* The mixin receives two parameters */
@mixin
bordered
(
$color
,
$width
)
{
border
:
$width
solid
$color
;
}
.myArticle
{
@include
bordered
(
blue
,
1px
)
;
// Call the mixin and pass two parameters
}
.myNotes
{
@include
bordered
(
red
,
2px
)
;
// Call the mixin and pass two parameters
}
The mixin parameters in the above example are the border properties (color and width).
Convert the above code into CSS code as shown below:
CSS code:
.myArticle
{
border
:
1px
solid
blue
;
}
.myNotes
{
border
:
2px
solid
red
;
}
Mixin parameters can also define default values, with the syntax format as follows:
Example
@mixin
bordered
(
$color
:
blue
,
$width
:
1px
)
{
border
:
$width
solid
$color
;
}
When including the mixin, you only need to pass the required 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
)
;
}
Convert the above code into CSS code as shown below:
CSS code:
p
{
border-color
:
blue
;
border-width
:
1in
;
border-style
:
dashed
;
}
h1
{
border-color
:
blue
;
border-width
:
2in
;
border-style
:
dashed
;
}
Variable arguments (varargs)
Sometimes we cannot determine how many parameters a mixin or a function uses; in such cases we can use ... to set variable arguments.
For example, a mixin used to create a box-shadow can take any number of box-shadow values 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
)
;
}
Convert the above code into CSS code as shown below:
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 mixins for browser prefixes
Using mixins for browser prefixes is also very convenient, as 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 into CSS code as shown below:
CSS code:
.myBox
{
-webkit-transform
:
rotate
(
20deg
)
;
-ms-transform
:
rotate
(
20deg
)
;
transform
:
rotate
(
20deg
)
;
}