The @charset at-rule declares the character encoding of a stylesheet. UTF-8 is the most common value and ensures international characters render correctly in external CSS files.
01
Encoding
UTF-8 default.
02
First line
Top of file.
03
At-rule
Not a property.
04
External
.css files only.
05
One rule
Per stylesheet.
06
Often optional
HTTP + meta.
Fundamentals
Introduction
The @charset at-rule in CSS declares the character encoding of a stylesheet. It tells the browser how to interpret the bytes in a CSS file so that letters, symbols, and Unicode characters display correctly.
Definition and Usage
@charset is not a CSS property — it is an at-rule, like @media or @import. It applies to the entire stylesheet, not to individual elements. In practice, UTF-8 is the encoding you will use almost every time.
The rule is mainly useful in external.css files. Browsers often ignore @charset inside embedded <style> tags because the HTML document already defines encoding through <meta charset="UTF-8"> or HTTP headers.
💡
Beginner Tip
Save CSS files as UTF-8 in your editor, serve them with charset=utf-8 in the Content-Type header, and add @charset "UTF-8"; as the first line when you want an explicit declaration in the file itself.
Foundation
📝 Syntax
The syntax for @charset is a quoted encoding name followed by a semicolon. It must appear at the very beginning of an external stylesheet:
syntax.css
@charset"UTF-8";
The encoding name is case-insensitive in CSS, but "UTF-8" is the conventional form. Only one @charset rule is permitted per file.
@charset "UTF-8";@charset "ISO-8859-1";One per file only
Placement Rules
@charset must be the first statement in an external CSS file.
In strict interpretation, nothing — not even comments — may appear before it.
Only one@charset rule is allowed per stylesheet.
It is not inherited and does not cascade to other stylesheets.
Embedded <style> blocks typically ignore @charset.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Type
At-rule (not a CSS property)
Common value
@charset "UTF-8";
Placement
First line of external .css file
Per file
One declaration only
Embedded <style>
Usually ignored
When optional
HTTP Content-Type charset or HTML meta charset=UTF-8
Browser support
100% for external stylesheets
Context
When to Use @charset
Reach for @charset in these situations:
External CSS files — Declare encoding explicitly at the top of standalone .css files.
Non-ASCII content in CSS — When your stylesheet contains Unicode in content values, font names, or string data.
Legacy servers — When HTTP headers do not reliably send charset=utf-8.
Cross-origin stylesheets — When encoding cannot be inferred from the HTML document.
Documentation clarity — Signal to other developers that the file is saved as UTF-8.
Skip @charset when your HTML already uses <meta charset="UTF-8">, your server sets the correct Content-Type, and your CSS file contains only ASCII characters.
Preview
👀 Live Preview
With UTF-8 encoding declared, international characters in CSS content and text render correctly:
@charset "UTF-8";Stylesheet encoding set to UTF-8 for full Unicode support.
Unicode in CSS content
External .css file patternstyles.css → @charset first, then rules
Hands-On
Examples Gallery
Practice @charset with UTF-8 declarations, Unicode content, full stylesheet structure, and the external file pattern.
🔠 Core Patterns
Start with the UTF-8 declaration and Unicode content examples.
Example 1 — UTF-8 Declaration
Declare UTF-8 encoding at the top of a stylesheet before any other CSS rules.
An external .css file is fetched via <link rel="stylesheet">.
Fetch
2
@charset is parsed first
If the first bytes match @charset "…";, that encoding is used for the file.
Parse
3
Fallback encoding applies
Without @charset, the browser uses HTTP headers, BOM, or UTF-8 as default.
Fallback
=
🌐
Correct text rendering
Unicode characters in CSS rules and content display as intended.
Compatibility
🖥 Browser Compatibility
The @charset at-rule has 100% browser support for external stylesheets in Chrome, Firefox, Safari, Edge, and Opera.
✓ Baseline · Universal support
@charset in external CSS
All major browsers honor @charset at the top of linked .css files.
100%External stylesheets
Google ChromeAll versions · External CSS
Full support
Mozilla FirefoxAll versions · External CSS
Full support
Apple SafariAll versions · External CSS
Full support
Microsoft EdgeAll versions · External CSS
Full support
OperaAll modern versions
Full support
@charset at-rule100% supported
Bottom line:@charset is safe for production in external stylesheets. Embedded <style> blocks are the exception — browsers ignore the rule there.
Wrap Up
🎉 Conclusion
The @charset at-rule declares how a stylesheet’s bytes should be interpreted. While often unnecessary when HTTP headers and HTML meta charset already specify UTF-8, it remains a useful explicit declaration in external CSS files — especially when your stylesheet contains international characters.
Remember: one rule per file, first line only, quoted encoding name, and UTF-8 for virtually every modern project.
Put @charset "UTF-8"; as the first line in external CSS files
Save CSS files as UTF-8 in your code editor
Configure server Content-Type with charset=utf-8
Use <meta charset="UTF-8"> in HTML documents
Declare encoding when CSS contains non-ASCII characters
❌ Don’t
Place comments or whitespace before @charset
Rely on @charset inside embedded <style> tags
Add more than one @charset rule per file
Confuse @charset with a regular CSS property
Declare UTF-8 while saving the file in a different encoding
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about @charset
Use these points when working with stylesheet encoding.
5
Core concepts
UTF-801
Encoding
Declares charset.
Purpose
1st02
First line
Before all rules.
Placement
@03
At-rule
Not a property.
Type
.css04
External
Linked files.
Scope
1×05
One only
Per stylesheet.
Limit
❓ Frequently Asked Questions
The @charset at-rule declares the character encoding of a stylesheet. It tells the browser how to interpret bytes in the CSS file — UTF-8 is the most common value and supports international characters, symbols, and emoji in CSS content.
In an external stylesheet, @charset must be the very first thing in the file — before any other rules and, in strict interpretation, even before comments. Only one @charset rule is allowed per file.
Often no. When the HTML document declares UTF-8 and the server sends Content-Type with charset=utf-8, external CSS is usually interpreted as UTF-8 without @charset. Add @charset when encoding might be ambiguous or when the CSS file uses non-ASCII characters.
Browsers typically ignore @charset in embedded <style> blocks because the encoding is inherited from the HTML document. @charset is designed for standalone external .css files linked with <link rel="stylesheet">.
Exactly one. If multiple @charset declarations appear, only the first valid one at the top of the file is used; additional declarations are ignored.