Path: blob/main/files/en-us/web/css/@charset/index.md
6510 views
------{{CSSRef}}
The @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 {{ HTMLElement("style") }} element where the character set of the HTML page is relevant.
This at-rule is useful when using non-ASCII characters in some CSS properties, like {{ cssxref("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) :
The value of the Unicode byte-order character placed at the beginning of the file.
The value given by the
charsetattribute of theContent-Type:HTTP header or the equivalent in the protocol used to serve the style sheet.The
@charsetCSS at-rule.Use the character encoding defined by the referring document: the
charsetattribute of the {{ HTMLElement("link") }} element. This method is obsolete and should not be used.Assume that the document is UTF-8
Syntax
Formal syntax
charset
: A {{cssxref("<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.
Examples
Valid and invalid charset declarations
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
Character set glossary entry
Unicode glossary entry