Skip to main content
HTML Validation

CSS: The @charset rule may only occur at the start of the style sheet. Please check that there are no spaces before it.

About This CSS Issue

The @charset rule in a <style> element is not valid because @charset only applies to external CSS files, not inline styles.

When CSS is embedded inside a <style> element, the character encoding is already determined by the HTML document itself (through the <meta charset> declaration or the HTTP Content-Type header). The @charset rule has no effect in this context, and the W3C validator flags it as an error.

This error also appears when @charset is present in an external .css file but is not on the very first line, or has a space or BOM (byte order mark) before it. In external stylesheets, @charset must be the absolute first thing in the file, with no preceding whitespace or comments.

How to fix it

If the @charset rule is inside a <style> element, remove it:

<!-- Wrong: @charset inside a style element -->
<style>
  @charset "UTF-8";
  body {
    color: #333;
  }
</style>

<!-- Fixed: remove the @charset rule -->
<style>
  body {
    color: #333;
  }
</style>

If the @charset rule is in an external stylesheet, make sure it is on the very first line with no preceding spaces, BOM characters, or comments:

@charset "UTF-8";
body {
  color: #333;
}

Find issues like this automatically

Rocket Validator scans thousands of pages in seconds, detecting HTML issues across your entire site.

Help us improve our guides

Was this guide helpful?
๐ŸŒ Trusted by teams worldwide

Validate at scale.
Ship accessible websites, faster.

Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.

Scheduled Reports
API Access
Open Source Standards
$7 / 7 days

Pro Trial

Full Pro access. Cancel anytime.

Start Pro Trial โ†’

Join teams across 40+ countries