Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 39 ยท Variables, Nesting and Partials

Sass Interpolation: The #{ } Syntax

Use Sass interpolation to build selectors, property names, strings, media queries and custom property values from variables, and know when it is needed.

Normally a Sass variable can stand in for a value: color: $brand. But what if you want a variable inside a selector, a property name, a string, or a media query? Those places are not value positions, so Sass needs an explicit signal. That signal is interpolation: write #{ ... } and Sass evaluates the expression inside and pastes the result into the surrounding text.

Interpolation in selectors

SCSS
$name: "warning";

.alert-#{$name} {
  color: orange;
}

This compiles to .alert-warning { color: orange; }. Combined with loops, interpolation is how Sass generates whole families of classes (.m-1, .m-2, ...) from one rule, as you will do in generating utility classes.

You can interpolate anywhere in a selector: class and ID names, attribute values, pseudo-classes with arguments such as :nth-child(#{$n}), and element names.

SCSS
$side: left;
$n: 3;

.item:nth-child(#{$n}) { margin-#{$side}: 0; }

Interpolation in property names

The same trick works for property names, which is perfect for directional properties:

SCSS
$sides: top, right, bottom, left;

@each $side in $sides {
  .pad-#{$side} {
    padding-#{$side}: 1rem;
  }
}

Each loop turn writes both a class name and a matching property (padding-top, padding-right, ...).

Interpolation in selectors and properties

Interpolation in strings

Inside a quoted string, #{ } inserts the value. Quotes in the inserted string are removed, so you do not end up with a doubled set:

SCSS
$font: "Inter";
$url: "images";

.hero {
  background: url("#{$url}/hero.jpg");
  --font-name: "#{$font} Variable";
}

The output is url("images/hero.jpg") and --font-name: "Inter Variable".

Interpolation in media queries and at-rules

A media query can mix variables and text, which is why many projects define named breakpoints:

SCSS
$breakpoint: 48em;

@media (min-width: #{$breakpoint}) {
  .nav { display: flex; }
}

Modern Sass allows a variable directly as a feature value, (min-width: $breakpoint), so interpolation is only required when you need to build part of the condition text. Common examples use it around the entire query or the feature name:

SCSS
$feature: "min-width";
$size: 600px;
@media (#{$feature}: #{$size}) { .x { color: red; } }

Interpolation in custom properties

CSS custom property values are treated as raw text, so Sass will not evaluate expressions inside them. Without #{ } you get the literal characters. This is the single most common interpolation mistake:

SCSS
$brand: #0ea5e9;
:root {
  --brand: $brand;        // wrong: output is literally "$brand"
  --brand: #{$brand};     // right: output is #0ea5e9
  --pad: #{$size * 2};    // right: math happens inside the braces
}

Keeping the slash in CSS shorthands

Since / is both division and a CSS separator (font: 16px/1.5), interpolation is the clean way to say "this is a slash, not a division":

SCSS
$size: 16px;
$line: 1.5;
.text { font: #{$size}/#{$line} Georgia, serif; }   // font: 16px/1.5 Georgia, serif

For grid lines or aspect-ratio, the slash between plain numbers is also kept as-is by modern Sass, but interpolation removes all doubt when variables are involved.

Interpolation and quotes

When you interpolate a quoted string outside another string, the quotes are removed. That makes #{ } a quick way to unquote:

SCSS
$family: "Helvetica Neue";
.a { font-family: #{$family}, sans-serif; }   // font-family: Helvetica Neue, sans-serif

That output works, but a font name with spaces should usually stay quoted for correctness. Prefer the string.unquote() function when you want to unquote deliberately, and keep quotes on names like "Helvetica Neue".

Custom properties, shorthand and media queries

Look at plain-property: $brand; in the output. That one is an ordinary property, so Sass substituted the variable as expected. It is only custom properties (the ones starting with --) that need the braces.

When you do not need interpolation

Beginners sometimes wrap every variable in #{ }. In most value positions that is unnecessary and can even change the result (it turns numbers into unquoted strings, so you cannot do math on them afterwards). Use it for:

  • selectors and property names,
  • inside quoted strings and comments,
  • custom property values,
  • building parts of media queries and @supports conditions,
  • slash-separated shorthands.

For plain values (width: $w, margin: $gap auto) just use the variable.

Interpolation returns an unquoted string, not a number. $a: #{10px}; width: $a * 2; fails, because $a is text. Do the math first, interpolate last.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does .icon-#{$name} compile to when $name is home?

    Sass evaluates the expression inside #{ } and pastes the result into the selector.
  2. 2. Which custom property line outputs the real value of $brand?

    Sass does not evaluate custom property values, so you need interpolation to inject the Sass value.
  3. 3. Where is interpolation required?

    In ordinary value positions you use variables directly. Selectors, property names and custom properties are text positions where #{ } is needed.
  4. 4. What type does an interpolated number become?

    Interpolation always produces an unquoted string, so you cannot do math on the result.

Summary

  • #{expression} evaluates an expression and inserts its result as text.
  • Use it in selectors, property names, strings, comments, media queries, slash shorthands and custom property values.
  • Do not use it for ordinary values; the result is an unquoted string, which cannot be used in math.
  • Do math first, interpolate last.

Next: Mixins