CSS @charset At-Rule

Beginner
⏱️ 5 min read
📚 Updated: Jul 2026
🎯 4 Examples
At-Rules

What You’ll Learn

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.

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.

📝 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.

Basic Example

charset-basic.css
@charset "UTF-8";

body {
  font-family: system-ui, sans-serif;
  color: #1e293b;
}
@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.

⚡ Quick Reference

QuestionAnswer
TypeAt-rule (not a CSS property)
Common value@charset "UTF-8";
PlacementFirst line of external .css file
Per fileOne declaration only
Embedded <style>Usually ignored
When optionalHTTP Content-Type charset or HTML meta charset=UTF-8
Browser support100% for external stylesheets

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.

👀 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 pattern styles.css → @charset first, then rules

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.

charset-utf8.css
@charset "UTF-8";

body {
  font-family: system-ui, sans-serif;
  padding: 1.5rem;
  color: #1e293b;
}
Try It Yourself

How It Works

The browser reads @charset "UTF-8"; first, then applies the remaining rules knowing the file uses UTF-8 encoding.

Example 2 — Unicode Content

Use @charset so special characters in CSS content properties render correctly.

charset-unicode.css
@charset "UTF-8";

.label::before {
  content: "© 2026 — café ";
  color: #2563eb;
  font-weight: 600;
}
Try It Yourself

How It Works

Without correct encoding, characters like ©, é, or ñ in content values may display as garbled symbols. UTF-8 prevents that.

📁 File Structure

Place @charset first, then write the rest of your stylesheet rules below it.

Example 3 — Full Stylesheet

Declare charset, then add reset rules, typography, and component styles.

charset-full.css
@charset "UTF-8";

* {
  box-sizing: border-box;
}

body {
  font-family: Georgia, serif;
  margin: 0;
  padding: 2rem;
  background: #f8fafc;
  color: #334155;
}

h1 {
  color: #1e293b;
}
Try It Yourself

How It Works

@charset is parsed once at load time. All subsequent rules inherit the declared encoding context for the file.

Example 4 — External File Pattern

See how @charset fits into a separate .css file linked from HTML.

charset-external.html
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">

/* styles.css — first line:
   @charset "UTF-8";
   body { font-family: system-ui, sans-serif; }
*/
Try It Yourself

How It Works

The HTML meta charset sets document encoding. The external CSS file uses its own @charset declaration for stylesheet-specific encoding when needed.

💬 Usage Tips

  • Save as UTF-8 — Configure your editor to save CSS files in UTF-8 without BOM when possible.
  • Server headers — Set Content-Type: text/css; charset=utf-8 on your web server.
  • First line only — Place @charset before comments, imports, and all other rules.
  • Quote the value — Use double quotes: @charset "UTF-8"; (single quotes also work in CSS).
  • Pair with HTML meta — Use both document-level and stylesheet-level encoding for maximum compatibility.

⚠️ Common Pitfalls

  • Comments before @charset — A comment on line 1 can invalidate the rule in strict parsers.
  • Using in <style> tags — Browsers ignore @charset in embedded stylesheets.
  • Multiple declarations — Only the first valid @charset is honored; duplicates are ignored.
  • Treating it as a property — You cannot write charset: "UTF-8" inside a rule block.
  • Wrong file encoding — Declaring UTF-8 while saving the file in a different encoding causes mojibake.

♿ Accessibility

  • Correct character display — Proper encoding ensures screen readers and assistive tech receive the intended text.
  • International content — UTF-8 supports accented characters and scripts used by global audiences.
  • Readable generated content — Unicode in content values must encode correctly for all users.
  • Language attributes — Pair encoding with lang attributes on HTML elements for pronunciation hints.
  • Avoid garbled symbols — Encoding errors create confusing replacement characters that harm comprehension.

🧠 How @charset Works

1

Browser loads stylesheet

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.

🖥 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 Chrome All versions · External CSS
Full support
Mozilla Firefox All versions · External CSS
Full support
Apple Safari All versions · External CSS
Full support
Microsoft Edge All versions · External CSS
Full support
Opera All modern versions
Full support
@charset at-rule 100% supported

Bottom line: @charset is safe for production in external stylesheets. Embedded <style> blocks are the exception — browsers ignore the rule there.

🎉 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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about @charset

Use these points when working with stylesheet encoding.

5
Core concepts
1st 02

First line

Before all rules.

Placement
@ 03

At-rule

Not a property.

Type
.css 04

External

Linked files.

Scope
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.

Practice in the Live Editor

Open the HTML editor, add @charset "UTF-8"; at the top of a stylesheet, and test Unicode characters in CSS content.

HTML Editor →

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