概述
@charset
CSS @规则 指定样式表中使用的字符编码。它必须是样式表中的第一个元素,而前面不得有任何字符。因为它不是一个嵌套语句,所以不能在@规则条件组中使用。如果有多个 @charset
@规则被声明,只有第一个会被使用,而且不能在HTML元素或HTML页面的字符集相关 <style>
元素内的样式属性内使用。
此 @规则 在某些 CSS 属性中使用非 ASCII 字符时非常有用,例如 content
。
在样式表中有多种方法去声明字符编码,浏览器会按照以下顺序尝试下边的方法(一旦找到就停止并得出结果):
- 文件的开头的 Unicode byte-order 字符值。
- 由Content-Type:HTTP header 中的 charset 属性给出的值或用于提供样式表的协议中的等效值。
CSS
@规则@charset。
- 使用参考文档定义的字符编码:
<link>
元素的 charset 属性。 该方法在 HTML5 标准中已废除,无法使用。 - 假设文档是 UTF-8。
语法
@charset "UTF-8"; @charset "iso-8859-15";
where:
- charset
- 它是一个
<string>
表示字符编码被使用。它必须是在被 IANA-registry 声明过的 web-safe 字符编码中的一个, 还必须被双引号包围, 遵循一个空格字符 (U+0020),并且立即以分号结束。 如果有多个相关的编码名字,只有被标记为 preferred 的那个才会被使用。
语法格式
@charset "<charset>";
例子
@charset "UTF-8";
@charset "utf-8"; /*大小写不敏感*/
/* 设置css的编码格式为Unicode UTF-8 */
@charset 'iso-8859-15'; /* 无效的, 使用了错误的引号 */
@charset 'UTF-8'; /* 无效的, 使用了错误的引号 */
@charset "UTF-8"; /* 无效的, 多于一个空格 */
@charset "UTF-8"; /* 无效的, 在at-rule之前多了一个空格 */
@charset UTF-8; /* Invalid, without ' or ", the charset is not a CSS <string>
*/
规范
Specification | Status | Comment |
---|---|---|
CSS Level 2 (Revision 1) @charset |
Recommendation | Initial definition |
浏览器兼容性
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@charset | Chrome Full support 2 | Edge Full support 12 | Firefox
Full support
1.5
| IE
Full support
5.5
| Opera Full support 9 | Safari Full support 4 | WebView Android Full support 2 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 4 | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
- See implementation notes.
- See implementation notes.