Sass Interpolation

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 5 Examples
#{expression}

What You’ll Learn

Sass interpolation embeds a SassScript expression into CSS with #{ }. This page covers where it works, when you should skip it, quoted-string quirks, safe number handling, and five compiled examples.

01

Syntax

#{expr}

02

Where

Selectors & more

03

Skip

Normal values

04

Strings

Quotes stripped

05

Units

Avoid #{}px

06

Practice

5 examples

What Is Sass Interpolation?

Official docs: wrap an expression in #{ } to inject its result into a chunk of CSS. Think of it as “paste this computed text here”—into a class name, a property name, a URL path, a keyframes name, or a loud comment.

  • Evaluates at compile time—the CSS file contains only the final text.
  • In SassScript, interpolation always produces an unquoted string.
  • Quoted strings lose their quotes when interpolated (even inside lists).
  • Rarely needed for plain property values—use the variable directly.
💡
Beginner tip

Ask: “Am I building CSS text (a name, a path, a selector piece)?” If yes, interpolate. If you only need a color, size, or other value, use $variable without #{ }.

📝 Syntax

styles.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);

🎯 Where You Can Use #{ }

Official docs list these locations:

  • Selectors in style rules
  • Property names in declarations
  • Custom property values
  • CSS at-rules (for example @media, @keyframes)
  • @extend targets
  • Plain CSS @imports
  • Quoted or unquoted strings
  • Special functions and plain CSS function names
  • Loud comments

⚠️ When Not to Interpolate

Official fun fact: other than injecting into strings, interpolation is rarely necessary in SassScript. You do not need it to use a variable as a property value.

AvoidPrefer
color: #{$accent};color: $accent;
width: #{$width}px;width: $width * 1px; (or store px on $width)
#{$string} only to unquotestring.unquote($string)
💡
Heads up — numbers

Interpolating a number returns an unquoted string. You lose further math and Sass’s unit safeguards. Bogus CSS like 10pxpx can slip through if $width already had units.

🧮 SassScript & Quoted Strings

Inside SassScript, interpolation injects text into unquoted strings—handy for dynamic animation names or slash-separated values. The result is always an unquoted string.

One special case: quotation marks around quoted strings are removed when interpolated. That lets you store selector-like text in a quoted string and inject it into a rule. When you only want to unquote, prefer string.unquote().

⚡ Quick Reference

GoalCode
Dynamic class.icon-#{$name}
Dynamic property#{$side}: 0;
Path in URLurl("/icons/#{$name}.svg")
Keyframes name@keyframes #{$name} { … }
Loud comment math/* sum = #{1 + 1} */
Safe unit attach$n * 1px (not #{$n}px)

Examples Gallery

Each example shows a core interpolation pattern. Open View Compiled CSS for verified output.

📚 Getting Started

Official-style mixin and everyday selector/property injection.

Example 1 — Corner Icon Mixin (Official Pattern)

Interpolate into a selector, a URL, and property names in one mixin.

styles.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

$name builds both the class and the file path. $top-or-bottom / $left-or-right become real CSS properties (top, left).

Example 2 — Class Names, Property Names & Custom Props

Build identifiers from variables when the name itself must change.

styles.scss
$side: left;
$prop: border-#{$side}-color;
$theme: dark;

.card-#{$theme} {
  #{$prop}: #036;
  --accent-#{$theme}: #c6538c;
}

How It Works

$prop is itself built with interpolation, then used as a property name. Custom properties can interpolate in the name as well.

📈 Strings, Motion & At-Rules

Quote stripping, @keyframes names, media queries, and loud comments.

Example 3 — Quoted Strings Lose Quotes

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

styles.scss
@use "sass:string";

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

How It Works

Both lines output an unquoted string. Prefer string.unquote when unquoting is the only goal; keep #{ } when you are injecting into a larger piece of CSS text.

Example 4 — Dynamic @keyframes Name

Inject a name into an at-rule, then reuse the same variable for animation-name.

styles.scss
@mixin pulse($name, $duration) {
  @keyframes #{$name} {
    from { opacity: 0.4; }
    to { opacity: 1; }
  }

  .pulse {
    animation-name: $name;
    animation-duration: $duration;
  }
}

@include pulse(fade-in, 1.5s);

How It Works

The at-rule needs #{$name} because it is CSS syntax, not a SassScript property value. Passing $name to animation-name needs no braces.

Example 5 — Media Query & Loud Comment

Interpolate into @media and into a loud comment.

styles.scss
$bp: 768px;

@media (min-width: #{$bp}) {
  .sidebar {
    display: block;
  }
}

/* Built at #{"compile"} time: #{1 + 1} */
.note {
  content: "ok";
}

How It Works

Modern Dart Sass also allows @media (min-width: $bp) without braces—both work. Inside the loud comment, #{"compile"} strips quotes and #{1 + 1} becomes 2.

🚀 Real-World Use Cases

  • BEM / theme classes.btn-#{$variant}.
  • Directional properties#{$side}-margin helpers.
  • Asset URLsurl("/icons/#{$name}.svg").
  • Generated motion — unique or parameterized @keyframes names.
  • Build banners — version or math inside loud comments.

🧠 How Compilation Works

1

Find #{ }

Locate interpolation in selectors, names, strings, at-rules.

Scan
2

Evaluate expression

Run SassScript inside the braces (vars, math, calls).

Compute
3

Inject as text

Paste the result (quotes stripped for quoted strings).

Inject
4

CSS ships

No #{ } remains—only concrete CSS text.

⚠️ Common Pitfalls

  • color: #{$accent} — unnecessary; use $accent.
  • #{$n}px — stringifies numbers; prefer $n * 1px.
  • Trying to build $theme-#{$name} variables — illegal; use a map.
  • Using #{ } only to unquote — call string.unquote instead.
  • Expecting runtime changes — interpolation is compile-time only.

💡 Best Practices

✅ Do

  • Interpolate when building selectors, property names, and paths
  • Keep units on numbers with Sass math (* 1px)
  • Use string.unquote when unquoting is the only goal
  • Pass plain variables into property values without braces
  • Document why a name must be dynamic (theme, side, variant)

❌ Don’t

  • Wrap every variable in #{ } “just in case”
  • Glue units with string interpolation
  • Invent variable names via interpolation
  • Lose type safety by stringifying values you still need to compute with
  • Forget that quoted strings lose their quotes inside #{ }

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass interpolation

Inject text wisely—skip it for ordinary values and units.

5
Core concepts
🎯 02

Names & paths

best use cases

Where
03

Not for values

use $var bare

Skip
🔢 04

No #{}px

use * 1px

Units
05

Quotes drop

or unquote()

Strings

❓ 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. Official docs: 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.
Did you know?

Official Sass docs recommend building unique animation names with interpolation plus unique-id() (or meta.unique-id()) so generated keyframes never collide when a mixin is included many times.

Conclusion

Sass interpolation pastes compiled SassScript into CSS text with #{ }. Use it for names and paths; skip it for ordinary values; never glue units with string interpolation.

Continue with Sass Syntax or Sass Comments.

Next: Sass Syntax

Compare SCSS (.scss) 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.

5 people found this page helpful