<string>

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

<string> CSS 数据类型表示一个字符序列。字符串用于许多 CSS 属性,例如 contentfont-familyquotes

语法

<string> 数据类型由任意数量的 Unicode 字符组成,这些字符用双引号 (") 或单引号 (') 括起来。

大多数字符都可以直接表示。所有字符也可以用它们各自的十六进制 Unicode 码点表示,在这种情况下,它们前面有一个反斜杠 (\)。例如,\22 表示双引号,\27 表示单引号 ('),\A9 表示版权符号 (©)。

重要的是,某些原本无效的字符可以用反斜杠进行转义。这些字符包括在双引号字符串中使用的双引号、在单引号字符串中使用的单引号以及反斜杠本身。例如,\\ 将创建一个反斜杠。

要输出换行符,必须使用换行符字符(如 \A\00000A)对其进行转义。然而,在您的代码中,字符串可以跨多行,在这种情况下,每行的新行都必须用 \ 作为行的最后一个字符进行转义。

但是,要获得换行符,您还必须将 white-space 属性设置为适当的值。

注意: 字符引用(如 &nbsp;&#8212;)不能在 CSS <string> 中使用。

示例

有效字符串的示例

css
/* Basic strings */
"This string is demarcated by double quotes."
'This string is demarcated by single quotes.'

/* Character escaping */
"This is a string with \" an escaped double quote."
"This string also has \22 an escaped double quote."
'This is a string with \' an escaped single quote.'
'This string also has \27 an escaped single quote.'
"This is a string with \\ an escaped backslash."

/* New line in a string */
"This string has a \Aline break in it."

/* String spanning two lines of code (these two strings will have identical output) */
"A really long \
awesome string"
"A really long awesome string"

规范

规范
CSS 值和单位模块第 4 级
# 字符串

浏览器兼容性

另见