tags:

views:

119

answers:

2

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
so is it always good to use it?
metal-gear-solid
is it works in IE?
metal-gear-solid
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
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
According to http://msdn.microsoft.com/en-us/library/cc351024.aspx it works in Internet Explorer 5.5 and later.
Joey
Pekka: `content` and `quotes` spring to my mind, yes. And comments in your native language.
Joey
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
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
What will happen with my css if i do not provide @charset at top?
metal-gear-solid
Safari has also problem with @charset http://www.designerstalk.com/forums/help-me/49808-safari-4-css-problem-display-none.html
metal-gear-solid
and this link also saying that @charset is buggy in all IE versions http://reference.sitepoint.com/css/at-charset
metal-gear-solid
+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
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
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