Sass Interpolation

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Interpolation embeds a SassScript expression into a chunk of CSS. Wrap the expression in #{ } inside selectors, property names, URLs, custom properties, strings, and more.

Official sample idea.

.icon-#{$name} and url("/icons/#{$name}.svg") inject the value of $name into CSS text.

Not for ordinary values.

Write color: $accent—not color: #{$accent}. Interpolation is for CSS syntax, not plain SassScript values.

Avoid #{$width}px.

Interpolation returns an unquoted string and skips unit safeguards. Prefer $width * 1px (or declare $width with units).

Syntax

SCSS
// Inject into CSS text:
.icon-#{$name} { ... }
#{$side}: 0;
url("/icons/#{$name}.svg");
--brand: #{$accent};

// Ordinary property values — no interpolation needed:
color: $accent;

// In SassScript, #{ } always returns an unquoted string:
$name: inline-#{unique-id()};

Pattern: build a CSS identifier / string / at-rule fragment → wrap the dynamic part in #{ } → compile to plain CSS.

Where It Works

Places you can use #{ }:

PlaceExample idea
Selectors.icon-#{$name}
Property names#{$top-or-bottom}: 0
Custom property values--brand: #{$accent}
URLs / strings / at-rulesurl("/icons/#{$name}.svg")
SassScript stringsAlways returns an unquoted string

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Corner Icon Mixin

Interpolate into a class name, a URL, and property names.

SCSS
@mixin corner-icon($name, $top-or-bottom, $left-or-right) {
  .icon-#{$name} {
    background-image: url("/icons/#{$name}.svg");
    position: absolute;
    #{$top-or-bottom}: 0;
    #{$left-or-right}: 0;
  }
}

@include corner-icon("mail", top, left);

How It Works

  1. Selector becomes .icon-mail.
  2. URL embeds the same name.
  3. Property names become top / left.
  4. Matches the official Sass docs sample.

Example 2 — Dynamic Class Name

Build a modifier class from a variable.

SCSS
$mod: primary;

.btn-#{$mod} {
  border-color: currentColor;
}

How It Works

  1. $mod is SassScript.
  2. #{ } injects it into the selector text.
  3. Output is a normal CSS class.
  4. Common for BEM-style generators.

Example 3 — Custom Property Values

Custom properties need interpolation—plain $var is emitted literally.

SCSS
$accent: #336699;

:root {
  --brand: #{$accent};
}

How It Works

  1. Custom props are special in Sass.
  2. Without #{ }, CSS would keep $accent as text.
  3. With #{ }, the resolved color is written.
  4. Contrast with color: $accent (no braces needed).

Example 4 — Quoted Strings Lose Quotes

Interpolation strips quotation marks; string.unquote is clearer for that job alone.

SCSS
@use "sass:string";

.example {
  unquoted: #{"string"};
  clearer: string.unquote("string");
}

How It Works

  1. #{"string"} injects without quotes.
  2. Same CSS from string.unquote.
  3. Prefer unquote when unquoting is the only goal.
  4. Useful when quoted text holds selector-like syntax.

Example 5 — Prefer Math Over #{$n}px

Both can look the same in CSS—only the math-safe form keeps units correct.

SCSS
$width: 10;

.bad {
  width: #{$width}px;
}

.good {
  width: $width * 1px;
}

How It Works

  1. Interpolation builds a string 10px.
  2. Math keeps a real length value.
  3. Unit errors surface with math, not with strings.
  4. Best: declare $width: 10px from the start.

Edge Cases & Pitfalls

Most interpolation bugs are using #{ } where plain SassScript already works.

values

Skip #{ } for normal properties

color: $accent is enough. Extra braces add noise, not power.

numbers

Do not build units with strings

#{$width}px loses math. Use $width * 1px or unit-bearing variables.

unquote

Prefer string.unquote to strip quotes

See string.unquote() when that is the only goal.

custom props

Custom props need #{ }

Without interpolation, Sass may emit the $variable name as literal CSS text.

Sass Support

Interpolation is core Sass syntax on Dart Sass.

TopicStatus
#{expression}Supported in Dart Sass for CSS text injection
SassScript resultAlways an unquoted string
Quoted stringsQuotes are removed when interpolated
Ordinary valuesUse the variable directly—no braces

Compile with the Dart Sass CLI.

Key Takeaways

  • #{ } injects SassScript into CSS text.
  • Selectors, names, URLs, custom props — yes.
  • Ordinary values & unit math — usually no.
  • Returns unquoted strings in SassScript.

One line: use #{expression} to embed SassScript into CSS identifiers and strings—not into ordinary property values.

Compile Notes

Practical checklist:

checklist.txt
1. Need a dynamic selector / property name / URL? Use #{ }
2. Need a normal property value? Use $var directly
3. Need a custom property value? Use #{$var}
4. Need units? Prefer math ($n * 1px), not #{$n}px
5. Need only to unquote? Prefer string.unquote()

Frequently Asked Questions

Interpolation embeds a SassScript expression into CSS by wrapping it in #{ }. Example: .icon-#{$name} becomes .icon-mail when $name is mail.
Almost anywhere CSS text is written: selectors, property names, custom property values, CSS at-rules, @extend, plain CSS @import, quoted or unquoted strings, special functions, plain CSS function names, and loud comments.
No. Instead of color: #{$accent}, write color: $accent. Interpolation is for injecting values into identifiers, strings, and other CSS syntax—not ordinary SassScript values.
Interpolation returns an unquoted string, so you lose Sass math and unit checks. Prefer $width * 1px, or declare $width with px from the start.
Yes. #{"string"} injects string without quotes. Prefer string.unquote($string) when your goal is only to unquote.
In SassScript, interpolation always returns an unquoted string. That is useful for dynamic names and slash-separated values, but not for further math.

Next: Sass Syntax

Compare SCSS braces with the indented .sass syntax.

Sass syntax →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful