What is the benefit to add @charset "ISO-8859-15"; or @charset "utf-8"; at top in css?
+3
A:
It specifies the character encoding of that CSS file, and thus how the browser should read it (strictly, how the browser should interpret the bytes making up that CSS file into characters and thus strings).
EDIT: Obligatory link to Joel Spolsky's character encoding article included, to clarify any issues on encodings.
Brian Agnew
2009-12-13 11:57:17
so is it always good to use it?
metal-gear-solid
2009-12-13 11:58:30
is it works in IE?
metal-gear-solid
2009-12-13 11:59:14
I would always try and use such things to avoid confusion. As to whether it works in IE, I don't know, but I would expect so (that linked document is authored by someone from Microsoft, btw :-)
Brian Agnew
2009-12-13 12:00:40
When would I need non-ascii text in a style sheet, anyway? The only instances I can think of are wacky URLs, font names, and :after contents, anything else?
Pekka
2009-12-13 12:03:23
According to http://msdn.microsoft.com/en-us/library/cc351024.aspx it works in Internet Explorer 5.5 and later.
Joey
2009-12-13 12:03:36
Pekka: `content` and `quotes` spring to my mind, yes. And comments in your native language.
Joey
2009-12-13 12:04:11
my question is not about what does charset i'm asking what benefits we will get if we add this at top of css? till dat i never used this. so if i use from now as a first thing in css file. what benefit will i get in terms of cross browser compatibility, css validation
metal-gear-solid
2009-12-13 12:13:43
Don't forget that browsers will be running in different environments with different default character sets. Without that header the browser is at liberty to interpret your CSS file in its default encoding, which may not be *your* default encoding.
Brian Agnew
2009-12-13 12:15:03
What will happen with my css if i do not provide @charset at top?
metal-gear-solid
2009-12-13 12:33:11
Safari has also problem with @charset http://www.designerstalk.com/forums/help-me/49808-safari-4-css-problem-display-none.html
metal-gear-solid
2009-12-13 15:51:10
and this link also saying that @charset is buggy in all IE versions http://reference.sitepoint.com/css/at-charset
metal-gear-solid
2009-12-13 15:53:50
+1
A:
It's unnecessary. The css file will use the encoding specified in the HTML document that calls it, and an HTML page needs to specify the charset to be valid. You would only need it in rare cases where you want a different encoding for your CSS.
Isley Aardvark
2009-12-14 01:33:06
but see "Brian Agnew" comment #8 in belo answer he wrote "Don't forget that browsers will be running in different environments with different default character sets. Without that header the browser is at liberty to interpret your CSS file in its default encoding, which may not be your default encoding"
metal-gear-solid
2009-12-14 03:40:32
That is true, but "your default encoding" should be declared in the HTML. If it isn't, your HTML won't validate, then you should be working on the validation instead of a (possibly unnecessary) band-aid fix in the CSS. Plus you're risking the previously mentioned nasty Safari bug. You should definitely be aware of character encoding, but it should be handled in the HTML.
Isley Aardvark
2009-12-14 18:24:06