What is Interpolation in Sass and when would you use it?
Experience Level:
Junior
Tags:
Sass
Answer
Interpolation can be used to embed the result of Sass expression into a CSS.
Interpolation is often used to generate selector names that are partially defined by a parameter. Another use case can be injecting icon names into URLs based on parameter values.
Sass
@mixin button($selector, $bgColor) {
.button-#{$selector} {
background-color: $bgColor;
color: white;
border: none;
}
}
@include button("primary", blue);
@include button("secondary", navy);
@include button("error", red);
CSS
.button-primary {
background-color: blue;
color: white;
border: none;
}
.button-secondary {
background-color: navy;
color: white;
border: none;
}
.button-error {
background-color: red;
color: white;
border: none;
}
Related Sass job interview questions
-
What is scale-color() function good for in Sass?
Sass Junior -
What are lighten() and darken() functions good for in Sass?
Sass Junior -
In Sass, can you declare two different variables that have the same name and different scope? Describe what happens in such situation.
Sass Junior -
What Sass variable scopes do you know and what are the differences between them?
Sass Junior -
In Sass, what comments are compiled into CSS and what comments are not?
Sass Junior