8.7 KiB
title | table_of_contents | introduction |
---|---|---|
Strings | true | Strings are sequences of characters (specifically [Unicode code points](https://en.wikipedia.org/wiki/Code_point)). Sass supports two kinds of strings whose internal structure is the same but which are rendered differently: [quoted strings](#quoted), like `"Helvetica Neue"`, and [unquoted strings](#unquoted) (also known as *identifiers*), like `bold`. Together, these cover the different kinds of text that appear in CSS. |
{% funFact %}
You can convert a quoted string to an unquoted string using the
string.unquote()
function, and you can convert an unquoted string to a
quoted string using the string.quote()
function.
{% codeExample 'fun-fact-strings', false %} @use "sass:string";
@debug string.unquote(".widget:hover"); // .widget:hover
@debug string.quote(bold); // "bold"
===
@use "sass:string"
@debug string.unquote(".widget:hover") // .widget:hover
@debug string.quote(bold) // "bold"
{% endcodeExample %} {% endfunFact %}
Escapes
All Sass strings support the standard CSS escape codes:
-
Any character other than a letter from A to F or a number from 0 to 9 (even a newline!) can be included as part of a string by writing
\
in front of it. -
Any character can be included as part of a string by writing
\
followed by its Unicode code point number written in hexadecimal. You can optionally include a space after the code point number to indicate where the Unicode number ends.
{% codeExample 'escapes', false %} @debug """; // '"' @debug .widget; // .widget @debug "\a"; // "\a" (a string containing only a newline) @debug "line1\a line2"; // "line1\a line2" @debug "Nat + Liz \1F46D"; // "Nat + Liz 👭"
@debug """ // '"' @debug .widget // .widget @debug "\a" // "\a" (a string containing only a newline) @debug "line1\a line2" // "line1\a line2" (foo and bar are separated by a newline) @debug "Nat + Liz \1F46D" // "Nat + Liz 👭" {% endcodeExample %}
{% funFact %} For characters that are allowed to appear in strings, writing the Unicode escape produces exactly the same string as writing the character itself. {% endfunFact %}
Quoted
Quoted strings are written between either single or double quotes, as in
"Helvetica Neue"
. They can contain interpolation, as well as any unescaped
character except for:
\
, which can be escaped as\\
;'
or"
, whichever was used to define that string, which can be escaped as\'
or\"
;- newlines, which can be escaped as
\a
(including a trailing space).
Quoted strings are guaranteed to be compiled to CSS strings that have the same
contents as the original Sass strings. The exact format may vary based on the
implementation or configuration—a string containing a double quote may be
compiled to "\""
or '"'
, and a non-ASCII character may or may not be
escaped. But that should be parsed the same in any standards-compliant CSS
implementation, including all browsers.
{% codeExample 'quoted', false %} @debug "Helvetica Neue"; // "Helvetica Neue" @debug "C:\Program Files"; // "C:\Program Files" @debug ""Don't Fear the Reaper""; // ""Don't Fear the Reaper"" @debug "line1\a line2"; // "line1\a line2"
$roboto-variant: "Mono"; @debug "Roboto #{$roboto-variant}"; // "Roboto Mono"
@debug "Helvetica Neue" // "Helvetica Neue" @debug "C:\Program Files" // "C:\Program Files" @debug ""Don't Fear the Reaper"" // ""Don't Fear the Reaper"" @debug "line1\a line2" // "line1\a line2"
$roboto-variant: "Mono" @debug "Roboto #{$roboto-variant}" // "Roboto Mono" {% endcodeExample %}
{% funFact %} When a quoted string is injected into another value via interpolation, its quotes are removed! This makes it easy to write strings containing selectors, for example, that can be injected into style rules without adding quotes.
{% endfunFact %}
Unquoted
Unquoted strings are written as CSS identifiers, following the syntax diagram below. They may include interpolation anywhere.