Home Sass String string.quote() Sass string.quote() Definition
Definition and Usage string.quote($string) returns $string as a quoted Sass string. Already-quoted input stays quoted. Load with @use "sass:string".
Official sample idea. string.quote(Helvetica) and string.quote("Helvetica") both become "Helvetica".
Why quote? Useful for font-family names, content strings, and normalizing mixed token inputs.
Opposite helper. string.unquote() removes quotes and can produce strings that are not valid CSS—use carefully.
Syntax
Syntax @use "sass:string";
string.quote($string) //=> quoted string
string.quote(Helvetica); // "Helvetica"
string.quote("Helvetica"); // "Helvetica"
// Common CSS uses:
font-family: string.quote(Roboto), sans-serif;
content: string.quote(New);
// Global alias (legacy): quote($string)
// Prefer string.quote after @use "sass:string" Pattern: pass any string → get a quoted string → emit safe CSS for fonts and content.
Code
SCSS Examples Examples use @use "sass:string". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. Font 3. vs unquote 4. Content 5. Loop Example 1 — Official Quote Samples Unquoted and already-quoted inputs both become quoted strings.
@use "sass:meta";
@use "sass:string";
.probe {
--from-unquoted: #{meta.inspect(string.quote(Helvetica))};
--from-quoted: #{meta.inspect(string.quote("Helvetica"))};
--type: #{meta.type-of(string.quote(Helvetica))};
} .probe {
--from-unquoted: "Helvetica";
--from-quoted: "Helvetica";
--type: string;
} How It Works Unquoted Helvetica becomes quoted.Already quoted stays quoted.type-of reports string.Matches the official Sass docs samples.Example 2 — Quote a Font Family Emit a quoted family name in font-family.
@use "sass:string";
.heading {
font-family: string.quote(Helvetica), sans-serif;
} .heading {
font-family: "Helvetica", sans-serif;
} How It Works Unquoted identifier enters the call.quote wraps it for CSS.sans-serif stays as the fallback.Common pattern for design tokens.Example 3 — Quote vs Unquote Same characters, different quote state.
@use "sass:meta";
@use "sass:string";
$raw: Helvetica;
$q: string.quote($raw);
$u: string.unquote($q);
.compare {
--quoted: #{meta.inspect($q)};
--unquoted: #{meta.inspect($u)};
} .compare {
--quoted: "Helvetica";
--unquoted: Helvetica;
} How It Works quote wraps the value.unquote removes the wrapping.inspect shows the difference clearly.Round-trip is useful for debugging.Example 4 — Quoted content Pass an unquoted label into a mixin that quotes it for CSS.
@use "sass:string";
@mixin label($name) {
.badge {
content: string.quote($name);
}
}
@include label(New); .badge {
content: "New";
} How It Works CSS content needs a string.Mixin accepts an unquoted label.quote guarantees quoted output.Safe API for generated badges.Example 5 — Quote Fonts in a Loop Build several rules from a list of family names.
@use "sass:string";
$names: Helvetica, Roboto, Georgia;
@each $name in $names {
.font-#{$name} {
font-family: string.quote($name), sans-serif;
}
} .font-Helvetica {
font-family: "Helvetica", sans-serif;
}
.font-Roboto {
font-family: "Roboto", sans-serif;
}
.font-Georgia {
font-family: "Georgia", sans-serif;
} How It Works List items start unquoted.quote wraps each font-family value.Class names stay unquoted via interpolation.Scales design-token font stacks. Edge Cases & Pitfalls Most quote bugs are confusing quote state with string content.
idempotent Already quoted stays quoted Calling quote twice does not double-wrap characters.
inspect Use meta.inspect to see quotes meta.inspect makes quote state obvious in custom properties.
unquote Unquote carefully Removing quotes can produce CSS that is not valid.
length Quotes are not characters string.length is the same for quoted and unquoted forms of the same text.
Toolchain
Sass Support Prefer the module form with current Dart Sass.
API Status string.quote()Dart Sass 1.23+ via @use "sass:string" Return type Quoted string Already quoted Stays quoted Legacy global quote($string)
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns a quoted string for CSS-friendly output.
Already quoted stays quoted — safe to normalize.
vs unquote — unquote removes quotes carefully.
Prefer @use "sass:string" (Dart Sass 1.23+).
One line: to force a quoted Sass string, use string.quote($string).
Practice
Compile Notes Practical checklist:
1. @use "sass:string"
2. Pass quoted or unquoted input
3. Expect a quoted string result
4. Use for font-family and content
5. Use unquote only when you truly need no quotes Frequently Asked Questions What does Sass string.quote() do? string.quote($string) returns $string as a quoted Sass string. Example: string.quote(Helvetica) becomes "Helvetica".
What if the value is already quoted? Quoting an already-quoted string still returns a quoted string. string.quote("Helvetica") is "Helvetica".
When do I need quotes in CSS? Common cases include font-family names (especially with spaces), content strings, and some identifiers that must appear with quotes in the output CSS.
How is it different from string.unquote()? quote adds/keeps CSS quotes around the value. unquote removes them and can produce strings that are not valid CSS—use unquote with caution.
Is there a global quote()? Yes. quote($string) is the legacy global name. Prefer string.quote after @use "sass:string".
Which compilers support the module API? Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:string with @use; they may still offer the legacy global name.
Next: Sass string.slice() Extract a portion of a string with 1-based indexes.
string.slice() → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion