TExcelTAD
Home › Sass › Sass Interpolation

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

scss · try it yourself
$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

scss · try it yourself
@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

scss · try it yourself
$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()

scss · try it yourself
$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?

  1. .btn-$size
  2. .btn-lg
  3. .btn-#{lg}
  4. A syntax error

Where is interpolation NOT needed to use a variable?

  1. In a selector
  2. In a property name
  3. In a plain property value like color: $c
  4. Inside a quoted string

Why is $n * 1px better than #{$n}px?

  1. It's shorter
  2. It keeps the result a number you can still calculate with
  3. Interpolation doesn't work with numbers
  4. It produces smaller CSS