What does the @include keyword do in Sass?
Experience Level: Junior
Tags: Sass
Answer
The @include keyword is used to apply a mixin. When the mixin is applied, its processed body is inserted into a selector.
In the example below, the mixin alert will be used two times. First its body will be inserted into the .alert-success selector, then it will be inserted into the .alert-error selector.
Sass:
@mixin alert($color, $bgColor) {
border: solid 1px $color;
color: $color;
background-color: $bgColor;
}
.alert-success {
@include alert(green, whitesmoke)
}
.alert-error {
@include alert(red, yellow)
}
The Sass from above will be compiled to the following CSS:
.alert-success {
border: solid 1px green;
color: green;
background-color: whitesmoke;
}
.alert-error {
border: solid 1px red;
color: red;
background-color: yellow;
}
Related Sass job interview questions
What is a difference between @mixin and @extend in Sass?
Sass JuniorWhat does @extend feature do in Sass?
Sass JuniorWhat is the advantage of using Sass mixins? When would you use them?
Sass JuniorWhat are Mixins in Sass, how do you define them and how do you use them?
Sass JuniorWhat are variables in Sass, how do you use them and what is the advantage of using variables?
Sass Junior