Hey! This is the dev blog for the app named gosnip.
If you need a neat way to organize your code, gosnip, it's free.

This blog talks about the how's and why's behind gosnip and features articles and tutorials on the techniques used.

CSS class names: allowed characters

Learned this the hard way: you cannot use underlines as the first character of a class name. This is very true. In fact, only IE6 has problems with it, but tell me of a time when that wasn’t enough. I thought I’d mark my utility css classes with a forward underscore (the clearfix and some default floats), but bloody IE6 does not accept that. So, I decided to go look for another character.

After Joe commented on my initial post (which was incomplete and very wrong), I did some soul-searching and started testing what characters you can use in a css class name.

valid? IE6 IE7 Firefox 2 Opera 9.26 SafariWin 3.1
dash valid no yes yes no yes
underscore valid no yes yes yes yes

Apart from the dash and the underline, you cannot use anything else, even if it’s other than the first character. The validator doesn’t say anything, but the browsers don’t apply the class. You can use numbers though, but not at the beginning. Oh, and they cannot be escaped anymore (by using the backslash \) as the css1 spec and the current official validator suggest.

So, the conclusion:

use a letter as the first character and remember that css is case sensitive

use letters, numbers, underscores and dashes for the rest of the characters, and still remember that css is case sensitive

When IE6 fades away, we will be able to use underscores as the first character too.

*thanks to Joe for pointing out my initial errors and for the impulse to do this “study”.

3 Responses to “CSS class names: allowed characters”

  1. Joe Says:

    Not true. I have been using underscores for a long time and I have had no issues.

  2. vlad Says:

    Hey! Thanks for the time you took to comment. I’ve updated the post now :) so it might be a bit more informative… and not that wrong.

  3. Simon B. Says:

    Be wary of underscore - and notice the second (humorous?) comment “some guy Eric Meyer”: http://archivist.incutio.com/viewlist/css-discuss/5380

    Oh, and unicode works:
    http://snook.ca/archives/html_and_css/unicode_for_css_class_names/

Leave a Reply