CSS @charset At-Rule

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

CSS Syntax

syntax.css
@charset "<charset-name>";

Double quotes, exactly one space after @charset, then a semicolon. No leading space on the line.

Rule Values

Value / formDescriptionTry it
"UTF-8"Recommended encoding for modern CSSTry it
with contentUseful when CSS includes non-ASCII charactersTry it
invalid quotesSingle quotes are not accepted for encoding detectionTry it
must be firstAnything before it makes the declaration failTry it
"iso-8859-15"Legacy encoding example (prefer UTF-8)Try it
extra spacesMore than one space after @charset is invalidTry it

How to Use @charset

Use it only in external stylesheets, with exact syntax.

  1. Save the .css file as UTF-8 (declaration alone is not enough).
  2. Put @charset "UTF-8"; as the first line of the file.
  3. Add @import (if any), then the rest of your CSS.
  4. Link the file with <link rel="stylesheet"> from HTML.
PieceRequired?Role
External .css fileYesWhere encoding detection applies
Exact byte sequenceYesDouble quotes + one space + semicolon
File saved as that encodingYesDeclaration must match real bytes
HTTP charset headerOften presentTakes precedence over @charset

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

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;
}
Try It Yourself

How It Works

  1. Start the file. No BOM required, but nothing else may precede the rule.
  2. Use exact syntax. Double quotes and one space after @charset.
  3. Save as UTF-8. The declaration must match the real file encoding.
  4. Write CSS next. Imports and rules come after this line.

Example 2 — Non-ASCII in content

UTF-8 keeps symbols and text in generated content readable.

CSS
@charset "UTF-8";

.label::before {
  content: "★ ";
}
Try It Yourself

How It Works

  1. Declare encoding first. Helps when the CSS file contains non-ASCII bytes.
  2. Use content. Generated text can include symbols and letters outside ASCII.
  3. Keep HTML as UTF-8 too. Matching encodings avoid surprises.
  4. Prefer UTF-8 always. Avoid legacy encodings unless you must.

Example 3 — Correct File Order

@charset comes first, then optional @import, then rules.

CSS
@charset "UTF-8";
@import url("base.css");

h1 {
  color: #5b21b6;
}
Try It Yourself

How It Works

  1. Charset first. Encoding must be known before the rest of the file is read.
  2. Imports second. Only @charset may precede @import.
  3. Rules last. Normal selectors and declarations follow.
  4. 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.

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 Chrome 2+
Full support
Mozilla Firefox 1.5+
Full support
Apple Safari 4+
Full support
Microsoft Edge 12+
Full support
Opera 10+
Full support
Samsung Internet 4+
Full support
Internet Explorer 6+
Full support
@charset Universally supported

Bottom line: Safe everywhere. Focus on exact syntax and UTF-8 files more than version numbers. Not a server runtime API.

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.

Next: @font-face

Learn how to load custom fonts with the @font-face at-rule.

@font-face tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful