Sass Interpolation
Interpolation inserts the result of a Sass expression into places where Sass would otherwise read plain text: selectors, property names, media queries, strings, calc() and even comments. The syntax is #{ expression }.
Interpolation in selectors
$component: "alert";
$variant: warning;
.#{$component} {
padding: 12px;
}
.#{$component}--#{$variant} {
background: #fff3cd;
}.alert {
padding: 12px;
}
.alert--warning {
background: #fff3cd;
}
Without #{}, .$component would be a syntax error — Sass doesn't look for variables inside selectors unless you ask it to.
Interpolation in property names
@mixin spacing($side, $size) {
margin-#{$side}: $size;
padding-#{$side}: $size * 0.5;
}
.note {
@include spacing(left, 16px);
@include spacing(top, 8px);
}.note {
margin-left: 16px;
padding-left: 8px;
margin-top: 8px;
padding-top: 4px;
}
Interpolation in strings and URLs
$asset-path: "/img";
$version: 3;
.logo {
background-image: url("#{$asset-path}/logo-v#{$version}.svg");
}
.label::after {
content: "Version #{$version}";
}.logo {
background-image: url("/img/logo-v3.svg");
}
.label::after {
content: "Version 3";
}
Interpolation in media queries and calc()
$tablet: 768px;
$header: 64px;
.main {
min-height: calc(100vh - #{$header});
@media (min-width: #{$tablet}) {
display: grid;
}
}.main {
min-height: calc(100vh - 64px);
}
@media (min-width: 768px) {
.main {
display: grid;
}
}
Note: Modern Sass understands variables inside
calc()without interpolation (calc(100vh - $header)works too), and media queries accept plain variables (@media (min-width: $tablet)). Interpolation is still common in existing code and always works.
Where you need interpolation
| Context | Need #{}? |
Example |
|---|---|---|
| Property value | No | color: $brand; |
| Selector | Yes | .icon-#{$name} |
| Property name | Yes | border-#{$side}: 0; |
| Inside a quoted string | Yes | "Hello #{$name}" |
@media query |
Optional | @media (min-width: $bp) |
Loud comment /* */ |
Yes | /* Theme: #{$theme} */ |
url() with a quoted path |
Yes | url("#{$path}/a.png") |
Quoted vs unquoted results
Interpolation removes quotes from quoted strings, which is why $component: "alert" produced the selector .alert and not ."alert". That makes it a handy way to unquote values, but for clarity in values prefer string.unquote() from sass:string.
Tip: Interpolation turns values into text. If you interpolate a number into a value (
width: #{$w}px), the result is an unquoted string, not a number, so you can't do math with it afterwards. Multiply instead:$w * 1px.
Test yourself
What does .btn-#{$size} compile to when $size: lg?
- .btn-$size
- .btn-lg
- .btn-#{lg}
- A syntax error
Where is interpolation NOT needed to use a variable?
- In a selector
- In a property name
- In a plain property value like
color: $c - Inside a quoted string
Why is $n * 1px better than #{$n}px?
- It's shorter
- It keeps the result a number you can still calculate with
- Interpolation doesn't work with numbers
- It produces smaller CSS