在 CSS 内容值中放置 Unicode 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10393462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing Unicode character in CSS content value
提问by davecave
I have a problem. I have found the HTML code for the downwards arrow, ↓
(↓)
我有个问题。我找到了向下箭头的 HTML 代码,↓
(↓)
Cool. Now I need to use it in CSS like so:
凉爽的。现在我需要像这样在 CSS 中使用它:
nav a:hover {content:"&darr";}
That obviously won't work since ↓
is an HTML symbol. There seems to be less info about these "escaped unicode" symbols that are used in css. There are other symbols like \2020
that I found but no arrows. What are the arrow codes?
这显然不起作用,因为它↓
是一个 HTML 符号。关于 css 中使用的这些“转义的 unicode”符号的信息似乎较少。\2020
我发现了其他类似的符号,但没有箭头。什么是箭头代码?
回答by Matt Ball
Why don't you just save/serve the CSS file as UTF-8?
为什么不将 CSS 文件保存/提供为 UTF-8?
nav a:hover:after {
content: "↓";
}
If that's not good enough, and you want to keep it all-ASCII:
如果这还不够好,并且您想将其全部保留为 ASCII:
nav a:hover:after {
content: "93";
}
The general format for a Unicode character inside a string is \000000
to \FFFFFF
– a backslash followed by six hexadecimal digits. You can leave out leading 0
digits when the Unicode character is the last character in the string or when you add a space after the Unicode character. See the spec below for full details.
一个字符串内使用Unicode字符的一般格式是\000000
到\FFFFFF
-反斜杠后跟六个十六进制数字。0
当 Unicode 字符是字符串中的最后一个字符或在 Unicode 字符后添加空格时,您可以省略前导数字。有关完整详细信息,请参阅下面的规范。
Relevant part of the CSS2 spec:
Third, backslash escapes allow authors to refer to characters they cannot easily put in a document. In this case, the backslash is followed by at most six hexadecimal digits (0..9A..F), which stand for the ISO 10646 ([ISO10646]) character with that number, which must not be zero. (It is undefined in CSS 2.1 what happens if a style sheet doescontain a character with Unicode codepoint zero.) If a character in the range [0-9a-fA-F] follows the hexadecimal number, the end of the number needs to be made clear. There are two ways to do that:
- with a space (or other white space character): "\26 B" ("&B"). In this case, user agents should treat a "CR/LF" pair (U+000D/U+000A) as a single white space character.
- by providing exactly 6 hexadecimal digits: "\000026B" ("&B")
In fact, these two methods may be combined. Only one white space character is ignored after a hexadecimal escape. Note that this means that a "real" space after the escape sequence must be doubled.
If the number is outside the range allowed by Unicode (e.g., "\110000" is above the maximum 10FFFF allowed in current Unicode), the UA may replace the escape with the "replacement character" (U+FFFD). If the character is to be displayed, the UA should show a visible symbol, such as a "missing character" glyph (cf. 15.2,point 5).
- Note: Backslash escapes are always considered to be part of an identifieror a string (i.e., "\7B" is not punctuation, even though "{" is, and "\32" is allowed at the start of a class name, even though "2" is not).
The identifier "te\st" is exactly the same identifier as "test".
第三,反斜杠转义允许作者引用他们不能轻易放入文档中的字符。在这种情况下,反斜杠后跟最多六个十六进制数字 (0..9A..F),代表具有该数字的 ISO 10646 ( [ISO10646]) 字符,该数字不能为零。(在 CSS 2.1 中未定义如果样式表确实包含具有 Unicode 代码点为零的字符会发生什么。)如果 [0-9a-fA-F] 范围内的字符跟在十六进制数字之后,则数字的结尾需要说清楚。有两种方法可以做到这一点:
- 带空格(或其他空白字符):“\26 B”(“&B”)。在这种情况下,用户代理应该将“CR/LF”对(U+000D/U+000A)视为单个空白字符。
- 通过提供正好 6 个十六进制数字:“\000026B”(“&B”)
其实这两种方法可以结合使用。十六进制转义后仅忽略一个空格字符。请注意,这意味着转义序列后的“真实”空间必须加倍。
如果数字超出 Unicode 允许的范围(例如,“\110000”高于当前 Unicode 中允许的最大 10FFFF),则 UA 可能会用“替换字符”(U+FFFD)替换转义符。如果要显示字符,UA 应显示可见符号,例如“缺少字符”字形(参见15.2,第 5 点)。
- 注意:反斜杠转义总是被认为是标识符或字符串的一部分(即,"\7B" 不是标点符号,即使 "{" 是,并且 "\32" 允许在类名的开头,即使虽然“2”不是)。
标识符“te\st”与“test”完全相同。
Comprehensive list: Unicode Character 'DOWNWARDS ARROW' (U+2193).