namespace
@charset
Declares the character encoding of the stylesheet so the parser can decode non-ASCII bytes correctly. It must be the very first thing in the file, byte for byte, and only utf-8 is meaningful in practice.
@charset "<encoding>"; Common conditions / descriptors
| Descriptor | Purpose |
|---|---|
| "utf-8" | The only encoding worth declaring today |
| double quotes | The encoding literal must be double-quoted |
| byte 0 | Must appear at the very first byte of the file, no BOM or whitespace before |
Examples
@charset "utf-8";
.brand::before { content: '\2014 '; } Declare UTF-8 so escaped or literal non-ASCII characters decode reliably
@charset "utf-8";
@import url('base.css'); @charset comes before @import, which comes before everything else
/* Wrong — a comment before @charset makes it a no-op */
/* theme file */
@charset "utf-8"; Any preceding byte disables the declaration
Gotcha
Any whitespace, comment, or BOM before @charset silently invalidates it. If the file is served with a correct HTTP Content-Type charset or a attribute, those win over @charset.
Related at-rules
@import
Pulls another stylesheet into the current one at parse time, optionally scoped to a cascade layer, supports condition, or media query. It must appear at the top of a stylesheet, before all other rules except @charset and @layer statements.
@layer
Declares one or more cascade layers so authors can control the order in which rule groups cascade, independent of specificity. Layers declared later win over layers declared earlier, and unlayered styles win over all named layers.
@page
Styles the page box used when printing or generating paged media, controlling size, margins, and margin boxes like headers and footers. Selectors like :first, :left, :right, and named pages let you target specific pages.
@property
Registers a typed CSS custom property so the engine knows its syntax, whether it inherits, and its initial value. This unlocks animation and transition of custom properties and rejects invalid values instead of falling back to the string type.