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”.