Revision 41638 of @charset

  • Revision slug: CSS/@charset
  • Revision title: @charset
  • Revision id: 41638
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment minor clean up; 11 words added, 8 words removed

Revision Content

Summary

The @charset CSS at-rule specifies the character encoding used in the style sheet. It must be the first element in the style sheet and not be preceded by any character. If several @charset at-rules are defined, only the first one is used, and it cannot be used inside a style attribute on an HTML element or inside the {{ HTMLElement("style") }} element where the character set of the HTML page is relevant.

This at-rule is useful when using non-ASCII characters in some CSS properties, like {{ cssxref("content") }}.

As there are several ways to define the character encoding of a style sheet, the browser will try the following methods in the following order (and stop as soon as one yields a result) :

  1. The value given by the charset attribute of the Content-Type: HTTP header or the equivalent in the protocol used to serve the style sheet.
  2. The value of the Unicode byte-order character placed at the beginning of the file.
  3. The @charset CSS at-rule.
  4. Use the character encoding defined by the referring document: the charset attribute of the {{ HTMLElement("link") }} element. This method is obsoleted in HTML5 and must not be used.
  5. Assume that the document is UTF-8.

Syntax

@charset charset

where :

charset
Is a {{ xref_cssstring() }} denoting the character encoding to be used. It must be the name of a web-safe character encoding defined in the IANA-registry. If several names are associated with an encoding, only the one marked with preferred must be used.

Examples

@charset "UTF-8"       // Set the encoding of the style sheet to Unicode UTF-8
@charset 'iso-8859-15' // Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign)
 @charset "UTF-8"      // Invalid, there is a character before the at-rule
@charset UTF-8         // Invalid, without ' or ", the charset is not a CSS <string>

Specifications

Specification Status Comment
CSS Level 2 (Revision 1) {{ Spec2('CSS2.1') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 2.0 {{ CompatGeckoDesktop("1") }}[*] 5.5 [**] 9 4
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 {{ CompatGeckoMobile("1") }} 5.5 [**] 10.0 4

[*] Firefox 1.0 supported only an invalid syntax where the character encoding is not set between single or double quotes.
[**] From IE 5.5 to IE 7 included, IE also supported the invalid syntax where the character encoding is not set between single or double quotes.

See also

Revision Source

<h2>Summary</h2>
<p>The <code>@charset</code> CSS at-rule specifies the character encoding used in the style sheet. It must be the first element in the style sheet and not be preceded by any character. If several <code>@charset</code> at-rules are defined, only the first one is used, and it cannot be used inside a <code>style</code> attribute on an HTML element or inside the {{ HTMLElement("style") }} element where the character set of the HTML page is relevant.</p>
<p>This at-rule is useful when using non-ASCII characters in some CSS properties, like {{ cssxref("content") }}.</p>
<p>As there are several ways to define the character encoding of a style sheet, the browser will try the following methods in the following order (and stop as soon as one yields a result) :</p>
<ol> <li>The value given by the <code>charset</code> attribute of the <code>Content-Type:</code> HTTP header or the equivalent in the protocol used to serve the style sheet.</li> <li>The value of the <a class="external" href="http://en.wikipedia.org/wiki/Byte_order_mark" title="http://en.wikipedia.org/wiki/Byte_order_mark">Unicode byte-order</a> character placed at the beginning of the file.</li> <li>The <code>@charset</code> CSS at-rule.</li> <li>Use the character encoding defined by the referring document: the <code>charset</code> attribute of the {{ HTMLElement("link") }} element. This method is obsoleted in HTML5 and must not be used.</li> <li>Assume that the document is UTF-8.</li>
</ol>
<h2>Syntax</h2>
<pre>@charset <em>charset</em>
</pre>
<p>where :</p>
<dl> <dt style="margin:0 40px"><em>charset</em></dt> <dd style="margin:0 40px">Is a {{ xref_cssstring() }} denoting the character encoding to be used. It must be the name of a web-safe character encoding defined in the <a class="external" href="http://www.iana.org/assignments/character-sets" rel="external" target="_blank" title="http://www.iana.org/assignments/character-sets">IANA-registry</a>. If several names are associated with an encoding, only the one marked with <em>preferred</em> must be used.</dd>
</dl>
<h2>Examples</h2>
<pre>@charset "UTF-8"       // Set the encoding of the style sheet to Unicode UTF-8
@charset 'iso-8859-15' // Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign)
 @charset "UTF-8"      // Invalid, there is a character before the at-rule
@charset UTF-8         // Invalid, without ' or ", the charset is not a CSS <a href="mks://localhost/en/CSS/string" title="string">&lt;string&gt;</a>
</pre>
<h2>Specifications</h2>
<table class="standard-table"> <thead> <tr style="background-color: rgb(255, 204, 255);"> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td><a class="external" href="http://www.w3.org/TR/CSS2/syndata.html#x57" title="http://www.w3.org/TR/CSS2/syndata.html#x57">CSS Level 2 (Revision 1)</a></td> <td>{{ Spec2('CSS2.1') }}</td> <td> </td> </tr> </tbody>
</table>
<h2 name="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>2.0</td> <td>{{ CompatGeckoDesktop("1") }}[*]</td> <td>5.5 [**] <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx"></a></td> <td>9</td> <td>4</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>2.1</td> <td>{{ CompatGeckoMobile("1") }}</td> <td>5.5 [**] <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx"></a></td> <td>10.0</td> <td>4</td> </tr> </tbody> </table>
</div>
<p>[*] Firefox 1.0 supported only an invalid syntax where the character encoding is not set between single or double quotes.<br> [**] From IE 5.5 to IE 7 included, IE also supported the invalid syntax where the character encoding is not set between single or double quotes.</p>
<h2>See also</h2>
<ul> <li><a href="/en/CSS/CSS_Reference" title="CSS Reference">CSS Reference</a></li> <li>{{ CSS_at_rules() }}</li>
</ul>
Revert to this revision