The @charset rule declares the character encoding of an external stylesheet. Use it so non-ASCII text in CSS (for example in content) decodes correctly.
Prefer UTF-8 for new stylesheets. Put @charset as the first bytes of the .css file — before comments and before @import. Only one declaration is allowed.
Warning — not a normal at-rule.
Encoding detection matches an exact byte sequence at the file start. Small syntax mistakes are ignored. HTTP Content-Type charset wins over @charset when present.
Often optional.
If HTML and CSS both use UTF-8, you may not need @charset. Keep it when encodings might differ or when teaching clear encoding intent.
Syntax
CSS Syntax
syntax.css
@charset"<charset-name>";
Double quotes, exactly one space after @charset, then a semicolon. No leading space on the line.
Use it only in external stylesheets, with exact syntax.
Save the .css file as UTF-8 (declaration alone is not enough).
Put @charset "UTF-8"; as the first line of the file.
Add @import (if any), then the rest of your CSS.
Link the file with <link rel="stylesheet"> from HTML.
Piece
Required?
Role
External .css file
Yes
Where encoding detection applies
Exact byte sequence
Yes
Double quotes + one space + semicolon
File saved as that encoding
Yes
Declaration must match real bytes
HTTP charset header
Often present
Takes precedence over @charset
Try it
Live Preview
@charset does not paint a box — it is a file encoding declaration. Compare valid and invalid forms below.
Encoding detection accepts only the exact valid byte sequence at the file start.
Declaration check
@charset "UTF-8";
Valid — exact form browsers recognize
Code
CSS Examples
Each snippet focuses on @charset. Use Try It Yourself for a live editor. Editors run inside HTML, so treat @charset as study syntax for external files.
Example 1 — UTF-8 at the Top
Declare UTF-8 as the first line of an external stylesheet.
CSS
@charset "UTF-8";
body {
font-family: Arial, sans-serif;
color: #0f172a;
}
Charset first. Encoding must be known before the rest of the file is read.
Imports second. Only @charset may precede @import.
Rules last. Normal selectors and declarations follow.
One charset only. Extra @charset lines are ignored.
Edge Cases & Pitfalls
Most failures come from tiny syntax mistakes at the top of the file.
syntax
Exact form only
Single quotes, missing quotes, leading spaces, or two spaces after @charset are ignored for encoding detection.
place
Must be the first bytes
A comment or blank line before it means the declaration does not count.
embed
Ignored in <style>
Embedded CSS uses the HTML document encoding. Put @charset in linked .css files.
http
HTTP charset wins
A Content-Type charset header overrides @charset. Keep them consistent.
save
Save the file as that encoding
Writing @charset "UTF-8"; in a Latin-1 file does not magically convert the bytes.
Compatibility
Browser Support
@charset is a browser CSS encoding declaration. Node.js, Deno, and Bun do not apply it as a layout API.
✓ Baseline · Long supported
@charset
Chrome 2+, Firefox 1.5+, Safari 4+, Edge 12+, Opera 10+, Internet Explorer 6+, Samsung Internet 4+.
99%All major browsers
Google Chrome2+
Full support
Mozilla Firefox1.5+
Full support
Apple Safari4+
Full support
Microsoft Edge12+
Full support
Opera10+
Full support
Samsung Internet4+
Full support
Internet Explorer6+
Full support
@charsetUniversally supported
Bottom line: Safe everywhere. Focus on exact syntax and UTF-8 files more than version numbers. Not a server runtime API.
Remember
Key Takeaways
Role: declare the character encoding of an external stylesheet.
Form:@charset "UTF-8"; — double quotes, one space, first line.
Scope: external .css files. Usually ignored inside <style>.
Tip: prefer UTF-8 everywhere; add @charset when encoding must be explicit.
One line:@charset "UTF-8"; is the first line of an external stylesheet when you need an explicit encoding.
Frequently Asked Questions
It declares the character encoding of an external stylesheet so the browser can decode bytes correctly. UTF-8 is the usual value.
It must be the very first bytes of an external .css file. Nothing may come before it except an optional UTF-8 BOM. Only one @charset is allowed.
Often no. If HTML and CSS share UTF-8 and the server sends a charset, browsers usually decode CSS correctly without @charset. Add it when encodings might differ or when non-ASCII characters appear in CSS.
Browsers typically ignore it in embedded style blocks. Encoding comes from the HTML document. @charset is for standalone external CSS files.
Use double quotes, exactly one space after @charset, and end with a semicolon: @charset "UTF-8"; Single quotes, extra spaces, or a leading space make it invalid.
It is Baseline and long supported: Chrome 2+, Firefox 1.5+, Safari 4+, Edge 12+, Opera 10+, Internet Explorer 6+. It is a browser CSS feature, not a Node.js API.