Intended audience: CSS coders, Web project managers, and anyone who wants to know how to declare the character encoding of a CSS file.
Updated 2010-08-07 6:36
How do I declare the character encoding of a CSS style sheet?
It is a good idea to always declare the encoding of external CSS style sheets if you have any non-ASCII text in your CSS file. For example, you may have non-ASCII characters in font names, in values of the content property, in selector values, etc.
For style declarations embedded in a document, @charset rules are not needed and must not be used. These rules are for use in linked style sheets only.
To set the character encoding inside the style sheet, use the @charset "at-rule". Its syntax is:
Only one @charset rule may appear in an external style sheet and it must appear at the very start of the document. It must not be preceded by any characters, not even comments. (However, a byte-order mark is OK for a document in one of the Unicode encodings.)
The name must be a charset name as described in the IANA registry. The IANA registry commonly includes multiple names for the same encoding. In this case you should use the name designated as preferred. For example to label your CSS file as UTF-8 encoded you would write:
You can also declare the encoding of the file in the HTTP Content-Type header, if you have a means to do so. For more information about how to set the encoding in HTTP see Setting the HTTP charset parameter. For example, this line in the HTTP response would indicate that the file is encoded in UTF-8.
Content-Type: text/css; charset=UTF-8
We recommend that if you use an HTTP declaration, you also include a declaration inside the style sheet. This will ensure that the encoding is still known if the style sheet is used locally or moved, eg. for testing or editing.
The declaration in the HTTP header will always override the in-document declaration, if there is a conflict.
The HTML 4.01 specification describes a charset attribute that can be added to the link element and is supposed to indicate the encoding of the document you are linking to. The use of this attribute on a link element is currently obsoleted by the HTML5 specification, however, so you shouldn't use it. Actually the charset attribute declaration is not supported by all browsers anyway, so that is another reason to avoid it.
Tell us what you think (English).
Content first published 2003-09-24. Last substantive update 2010-08-07 6:36 GMT. This version 2010-09-09 12:33 GMT
For the history of document changes, search for qa-css-charset in the i18n blog.
Copyright © 2003-2010 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark, document use and software licensing rules apply. Your interactions with this site are in accordance with our public and Member privacy statements.