CKTIS / CSS
@charset — CSS stylesheet encoding declaration
The @charset byte sequence can provide an external stylesheet encoding, but it is recognized only at the very start of the stream and never creates a CSSOM rule.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
@charset "<charset>";@charset "utf-8"; resembles an at-rule, but CSS Syntax states that no such rule exists. It is a strictly recognized byte sequence used while determining the encoding of an external stylesheet. The file bytes must begin exactly with @charset ", contain an encoding label in double quotes, and end with ";. A preceding comment or space, extra spacing, or single quotes prevents recognition as an encoding declaration.
An HTTP or equivalent protocol encoding label is considered before this byte pattern. Without one, the algorithm checks for the pattern within the first 1024 bytes; a BOM takes precedence during actual decoding. New stylesheets should use UTF-8 declared by transport or by the referring document. When neither is available, the file can start with a UTF-8 BOM or the exact @charset "utf-8"; characters. UTF-16 labels in this ASCII-compatible sequence resolve to UTF-8 because a truthful UTF-16 declaration could not itself have this byte spelling.
The example builds two real byte streams and loads them as external stylesheets through Blob URLs and <link> elements. Byte 0xB9 represents ą in windows-1250 but is not a valid standalone UTF-8 byte. With the exact declaration at byte zero the result contains ą; after a leading comment displaces it, the browser falls back to UTF-8 and produces the replacement character. This exercises byte decoding, not merely CSSOM parsing.
Examples
const encodedSheet = (leading, className) => {
const head = new TextEncoder().encode(leading + '@charset "windows-1250"; .' + className + '::before { content: "');
const tail = new TextEncoder().encode('"; }');
return new Uint8Array([...head, 0xb9, ...tail]); // 0xB9 is ą in windows-1250, invalid alone in UTF-8
};
const load = async (bytes, className) => {
const url = URL.createObjectURL(new Blob([bytes], {type: 'text/css'}));
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
document.head.append(link);
await new Promise((resolve, reject) => { link.onload = resolve; link.onerror = reject; });
const probe = document.createElement('div');
probe.className = className;
document.body.append(probe);
const content = getComputedStyle(probe, '::before').content;
URL.revokeObjectURL(url);
return content;
};
const exact = await load(encodedSheet('', 'exact'), 'exact');
const displaced = await load(encodedSheet('/*leading*/', 'displaced'), 'displaced');
console.log('exact=' + exact + '; displaced=' + displaced);Result
exact="ą"; displaced="�"
The @charset byte sequence can provide an external stylesheet encoding, but it is recognized only at the very start of the stream and never creates a CSSOM rule.