@charset

Spec
Version
CSS2
Browser support (more…)
IE8+ FF1.5+ SA4+ OP9.2+ CH2+
Full Full Full Full Full

Syntax

@charset "encoding";

Description

We use the @charset at-rule to specify the character encoding of an external style sheet. The at-rule must be followed by a quoted string value and a semicolon; the string must contain a valid encoding name from the IANA registry.

For obvious reasons, if it’s present, an @charset rule must be the very first thing in the CSS file. The only item that can precede it is a Unicode byte order mark (BOM).

You’ll rarely need to use an @charset rule in your style sheets. A user agent can deduce the character encoding of a CSS style sheet in four different ways, and if all of those fail, it uses a default.

For an external style sheet, a user agent will look for the following items:

  • a charset attribute in a Content-Type HTTP header (or similar) sent by the web server
  • a Unicode byte order mark, or an @charset at-rule
  • a charset attribute specified in the <link> tag from which the HTML document links to the style sheet
  • the encoding of the referring document or style sheet

This list defines the items in order of descending priority, and the first one that’s found will determine the style sheet’s encoding. If none are found, the user agent will assume the character encoding is UTF-8.

Tip: Using Special Characters

You can refer to characters that can’t be represented by the style sheet’s encoding using CSS escape notation.

Example

This example indicates that the style sheet will use the ISO-8859-15 character encoding:

@charset "ISO-8859-15";

Compatibility

Internet Explorer Firefox Safari Opera Chrome
5.5 6.0 7.0 8.0 1.0 1.5 2.0 3.0 3.5 1.3 2.0 3.1 4.0 9.2 9.5 10.0 2.0
Buggy Buggy Buggy Full Buggy Full Full Full Full None None None Full Full Full Full Full

In Internet Explorer versions up to and including 7, an @charset rule will not fail if the encoding is specified without quotes, even though it should.

In Firefox 1.0, an @charset rule will work only if it’s specified without quotes, on which it should actually fail.

User-contributed notes

ID:
#1
Contributed:
by Paul O'B
Date:
Wed, 24 Mar 2010 14:19:11 GMT

Safari loses the first rule is you add a space or comment before the charset:

https://bugs.webkit.org/show_bug.cgi?id=18265

Related Products