@charset Rule
The CSS @charset syntax rule is used to
declare the character encoding of the stylesheet.
It allows you to specify the desired character encoding for the stylesheet.
- Stylesheet
- A set of CSS rules used to control the layout and design of a page or document.
Both CSS files and the content inside the<style>tag are referred to as "stylesheets".
In general, stylesheets embedded within an HTML page share the character encoding of the entire document.
However, stylesheets linked to external CSS files (e.g., <link rel="stylesheet" href="styles.css">) can have their character encoding determined separately from that of the HTML document. When @charset is used, the encoding specified by @charset is provided as fallback encoding information to the algorithm that determines the stylesheet's character encoding. The algorithm follows several rules, and if the fallback encoding information can be used effectively in this process, that encoding is selected as the fallback encoding.
The specification treats @charset as having the same form as a CSS syntax construct that begins with @, but describes it only as providing fallback encoding information, rather than specifying a syntax construct like a typical CSS rule.
@charset is used to explicitly specify the character encoding of a stylesheet linked to an external CSS file, for whatever reason.
It is particularly useful in environments where the default encoding of the HTML file or system is not set to the standard UTF-8, allowing you to independently set the stylesheet's internal encoding to UTF-8.
The following is an example of setting the internal encoding of a stylesheet to UTF-8 using the @charset rule:
@charset "UTF-8"; /* Must be placed at the very beginning of the stylesheet */
/* CSS rules should be placed after this rule */
body {
font-size: 18px;
}
...
Syntax
@charset "charset-name";
charset-name represents the character encoding to be specified. It must be the name of a web-safe character encoding registered in the IANA registry. It must be separated from @charset by exactly one space, enclosed in double quotes ("), and immediately followed by a semicolon (;). It is case-insensitive.
The following usage rules also apply.
- The
@charsetrule must be placed at the very beginning of the stylesheet, and no characters, including whitespace, may appear before it. - The
@charsetrule can only be used once in a stylesheet. If multiple@charsetrules are used, only the first rule is applied. - The
@charsetrule is specific to stylesheets and cannot be replaced by thecharsetattribute of the<link>tag in an HTML document. - Stylesheets embedded in an HTML page, such as those within the
<style>tag or thestyleattribute, share the character encoding of the entire document. Therefore, even if the@charsetrule is used within the<style>tag, the stylesheet still uses the character encoding of the entire document.
Valid and Invalid Syntax
@charset "ISO-8859-1";
@charset "utf-8"; /* Same as "UTF-8": case-insensitive */
@charset 'ISO-8859-1'; /* Enclosed in single quotes */
@charset ISO-8859-1; /* Not enclosed in double quotes */
@charset "ISO-8859-1"; /* More than one whitespace character after @charset */
@charset "ISO-8859-1"; /* Characters, including whitespace, appear before the @charset rule */
Purpose of the @charset Rule
The @charset rule is used to explicitly specify the character encoding within a stylesheet, rather than relying on the encoding of an external environment that, for whatever reason, does not match the stylesheet's encoding.
It is recommended to use the standard encoding UTF-8 as the character encoding for stylesheets and HTML pages.
Therefore, there is no need to use the @charset rule in stylesheets.
Using the @charset Rule
Although it is ideal to always use UTF-8 as the character encoding for stylesheets and HTML documents, this is not always the case in practice.
For example, existing systems may use different encodings, or specific requirements may mandate the use of another encoding.
It is used when a stylesheet contains non-ASCII characters and UTF-8 cannot always be used as the character encoding for the stylesheet and HTML documents.
"Non-ASCII characters" refer to characters that are not included in the ASCII character set.
The ASCII character set includes the English alphabet, numbers, and certain special symbols, serving as a fundamental building block for many character encodings.
However, with the growth of diverse languages and internationalized content, characters outside the ASCII character set are also widely used. These characters belong to the Unicode character set, such as characters from various languages, emojis, and special symbols.
Stylesheets that use only ASCII characters do not require a separate character encoding declaration.
When UTF-8 Cannot Always Be Used as the Character Encoding for Stylesheets and HTML Pages
- Even if a web server uses a different character encoding, such as
ISO-8859-1, it is best practice to use the@charsetrule to explicitly specify that a CSS file usesUTF-8as its character encoding. - Even if a stylesheet file uses a character encoding other than
UTF-8, it is best practice to use the@charsetrule to explicitly specify the actual character encoding of the CSS file.
Browser Compatibility
| Syntax |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
@charset
|
2 | 12 | 1.5 | 4 |
Specifications
| Specification | |
|---|---|
@charset
|
CSS Syntax Module Level 3 #at-ruledef-charset |