From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/css/@charset/index.html | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 files/zh-cn/web/css/@charset/index.html (limited to 'files/zh-cn/web/css/@charset') diff --git a/files/zh-cn/web/css/@charset/index.html b/files/zh-cn/web/css/@charset/index.html new file mode 100644 index 0000000000..ae867c1752 --- /dev/null +++ b/files/zh-cn/web/css/@charset/index.html @@ -0,0 +1,79 @@ +--- +title: '@charset' +slug: Web/CSS/@charset +tags: + - '@charset' +translation_of: Web/CSS/@charset +--- +
{{ CSSRef }}
+ +
 
+ +

概述

+ +

 @charset CSS @规则  指定样式表中使用的字符编码。它必须是样式表中的第一个元素,而前面不得有任何字符。因为它不是一个嵌套语句,所以不能在@规则条件组中使用。如果有多个 @charset @规则被声明,只有第一个会被使用,而且不能在HTML元素或HTML页面的字符集相关 {{ HTMLElement("style") }} 元素内的样式属性内使用。

+ +

此 @规则 在某些 CSS 属性中使用非 ASCII 字符时非常有用,例如 {{ cssxref("content") }}。

+ +

在样式表中有多种方法去声明字符编码,浏览器会按照以下顺序尝试下边的方法(一旦找到就停止并得出结果):

+ +
    +
  1. 文件的开头的 Unicode byte-order 字符值。
  2. +
  3. 由Content-Type:HTTP header 中的 charset 属性给出的值或用于提供样式表的协议中的等效值。
  4. +
  5. CSS @规则  @charset。
  6. +
  7. 使用参考文档定义的字符编码: {{ HTMLElement("link") }} 元素的 charset 属性。 该方法在 HTML5 标准中已废除,无法使用。
  8. +
  9. 假设文档是 UTF-8。
  10. +
+ +

语法

+ +
@charset "UTF-8";
+@charset "iso-8859-15";
+
+ +

where:

+ +
+
charset
+
它是一个 {{cssxref("<string>")}} 表示字符编码被使用。它必须是在被 IANA-registry 声明过的 web-safe 字符编码中的一个, 还必须被双引号包围, 遵循一个空格字符 (U+0020),并且立即以分号结束。 如果有多个相关的编码名字,只有被标记为 preferred  的那个才会被使用。
+
+ +

语法格式

+ +
{{csssyntax}}
+
+ +

例子

+ +
@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 {{cssxref("<string>")}} */
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{ SpecName('CSS2.1', 'syndata.html#x57', '@charset') }}{{ Spec2('CSS2.1') }}Initial definition
+ +

浏览器兼容性

+ +

{{Compat("css.at-rules.charset")}}

-- cgit v1.2.3-54-g00ecf