Sub topic 6
Building selectors and values with #{}
Interpolation lets you insert the result of a Sass expression into places where Sass would otherwise treat your text literally. The syntax is #{expression}. This chapter assumes you know Sass variables, nesting, and basic CSS selectors and custom properties.
Why interpolation exists
Sass evaluates expressions in values, such as $a + $b. It does not evaluate expressions inside selectors, property names, or plain-CSS constructs like media query names. Interpolation fills that gap. Sass computes the expression, converts the result to an unquoted string, and pastes it in.
Building selectors
The most common use is generating class names from a variable or a loop.
$prefix: app;
.#{$prefix}-button {
padding: 0.5rem 1rem;
}
.#{$prefix}-card {
padding: 1rem;
}This compiles to:
.app-button {
padding: 0.5rem 1rem;
}
.app-card {
padding: 1rem;
}You can combine interpolation with a map and @each to emit one rule per entry.
$colors: (
primary: #3366cc,
danger: #cc3333
);
@each $name, $value in $colors {
.text-#{$name} {
color: $value;
}
}The output has .text-primary and .text-danger. Here $name is the key, and interpolation turns it into part of the selector.
Building property names
Interpolation also works in property names. This is useful when one mixin handles several sides of a box.
@mixin spaced($side, $amount) {
margin-#{$side}: $amount;
}
.panel {
@include spaced(top, 2rem);
}The result is margin-top: 2rem;.
Custom properties
CSS custom properties are a special case. Sass passes their values through as raw text, so a Sass variable placed there is not evaluated. You must interpolate it.
$primary: #3366cc;
:root {
--brand-wrong: $primary;
--brand: #{$primary};
}The compiled CSS contains --brand-wrong: $primary;, which is not valid, and --brand: #3366cc;, which works.
Media queries and other plain-CSS spots
Sass variables can be used directly in some media query positions, but interpolation is the safe choice when you build a whole query from parts.
$breakpoint: 48rem;
@media (min-width: #{$breakpoint}) {
.nav {
display: flex;
}
}Interpolation and quotes
Interpolation always produces an unquoted string. If you interpolate a quoted string into a value, the quotes are removed.
$font: "Helvetica Neue";
.title {
font-family: #{$font}, sans-serif;
}This outputs font-family: Helvetica Neue, sans-serif;. That works here, but a font name that needs quotes can break. Use the variable directly when you want to keep the quotes: font-family: $font, sans-serif;.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `.$prefix-button` | Sass reports a syntax error | Write `.#{$prefix}-button` |
| `--brand: $primary;` | Literal `$primary` in the CSS | Write `--brand: #{$primary};` |
| Interpolating a quoted string | Quotes are removed | Use the variable without `#{}` |
| Using `#{}` in arithmetic, such as `#{$a}+#{$b}` | You get a string, not a number | Do the math first, then interpolate the result if needed |
How it fits together
Exercise
Write the SCSS that sets --space: 8px; in :root from a Sass variable $space: 8px, and explain why --space: $space; would fail.
Show answer
Write :root { --space: #{$space}; }. Sass does not evaluate expressions in custom property values, so --space: $space; outputs the literal text $space, which the browser cannot use.
Exercise
Given $sides: (top, bottom);, write a loop that generates .m-top { margin-top: 1rem; } and .m-bottom { margin-bottom: 1rem; }.
Show answer
@each $side in $sides { .m-#{$side} { margin-#{$side}: 1rem; } }. The interpolation builds both the class name and the property name.