@charset
Example
/* styles */
@charset 'utf-8';@charset "UTF-8";
/* styles */@charset must be the first bytes of the file and use double quotes, or browsers skip it.
Common mistakes
- Placing
@charsetafter a comment or blank line, so it is ignored. - Using single quotes, which makes the rule invalid.
Key points
@charsetis the very first thing in the file, with double quotes and a semicolon.- Stylesheets are saved as UTF-8, served with a UTF-8
Content-Typeheader.
Interview questions
When is @charset needed?
@charset needed?Only when the CSS has non-ASCII characters and the server does not send a charset. A UTF-8 file with a correct header doesn't need it.
Can I use @charset in a <style> tag?
@charset in a <style> tag?No, it is ignored in inline styles and style attributes; the HTML document encoding applies.