@charset CSS at-rule specifies the character encoding used in the style sheet. It must be the first element in the style sheet and not be preceded by any character; as it is not a nested statement , it cannot be used inside conditional group at-rules . If several @charset at-rules are defined, only the first one is used, and it cannot be used inside a style attribute on an HTML element or inside the <style> element where the character set of the HTML page is relevant.

@charset "utf-8";
					

This at-rule is useful when using non-ASCII characters in some CSS properties, like content .

As there are several ways to define the character encoding of a style sheet, the browser will try the following methods in the following order (and stop as soon as one yields a result) :

  1. The value of the Unicode byte-order character placed at the beginning of the file.
  2. The value given by the charset 属性在 Content-Type: HTTP header or the equivalent in the protocol used to serve the style sheet.
  3. @charset CSS at-rule.
  4. Use the character encoding defined by the referring document: the charset 属性在 <link> element. This method is obsoleted in HTML5 and must not be used.
  5. Assume that the document is UTF-8

句法

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

where:

charset
<string> denoting the character encoding to be used. It must be the name of a web-safe character encoding defined in the IANA-registry , and must be double-quoted, following exactly one space character (U+0020), and immediately terminated with a semicolon. If several names are associated with an encoding, only the one marked with preferred must be used.

形式句法

@charset "<charset>";
					

范例

Valid and invalid charset declarations

@charset "UTF-8";       /* Set the encoding of the style sheet to Unicode UTF-8 */
@charset 'iso-8859-15'; /* Invalid, wrong quoting style used */
@charset  "UTF-8";      /* Invalid, more than one space */
 @charset "UTF-8";      /* Invalid, there is a character (a space) before the at-rule */
@charset UTF-8;         /* Invalid, without ' or ", the charset is not a CSS <string> */
					

规范

规范 状态 注释
CSS Level 2 (Revision 1)
The definition of '@charset' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
@charset Chrome 完整支持 2 Edge 完整支持 12 Firefox 完整支持 1.5 注意事项
完整支持 1.5 注意事项
注意事项 Firefox 1 supported an invalid syntax where the character encoding is not between single or double quotes.
IE 完整支持 5.5 注意事项
完整支持 5.5 注意事项
注意事项 From Internet Explorer 5.5 to IE 7 (inclusive), Internet Explorer supported an invalid syntax where the character encoding is not between single or double quotes.
Opera 完整支持 9 Safari 完整支持 4 WebView Android 完整支持 2 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 4 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

见实现注意事项。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Charsets
  4. At-Rules
    1. @charset