Are underscores OK in HTML class attributes?
Post made about 6pm on 17th August 2008 (a Sunday).
3 upstanding citizens have left thoughtful, constructive comments for this one. (Go see.)
Posted under Uncategorized
I’d been using underscores in class names for a while, and a couple of weeks ago someone I work on things with mentioned that they supposedly weren’t allowed. Now, I prefer underscores over hyphens generally (and certainly prefer them over using camel case), and Google have just pronounced them OK for URLs, too. Is there any good reason, in 2008, not to use underscores?
Molly Holzschlag let it be known a couple of weeks ago, on Twitter, that best practice involves not using underscores because of “irregular implementations”, and also mentions in neighbouring tweets that underscores were originally illegal in CSS2 (and had to be escaped in CSS1), but that they were made legal in the errata.
The only authoritative article I can find on the subject, and the one I think is being referenced here, was written by Eric Meyer in March 2001. It mentions that Netscape Navigator 4.x simply didn’t read underscores in class names, and neither did Opera 3.x-5.x. IE4.x upwards, more by luck than judgement, have recognised them fine.
So, unless somebody can stop me, I’m going to use them. Case, if not closed, then ajar.
More
You can skip to the end and leave a response.
Comment made at 11:52am, 3rd September 2008 (a Wednesday).
I use classes named without either but with a capital letter in the middle – there’s a name for this, but I can’t remember what it is… …so something like LogoContainer.
Comment made at 1:34pm, 8th September 2008 (a Monday).
It’s definitely down to personal preference. Personally, I like underscores in class/id names but many don’t.
I’m sure I came to use underscores as opposed to hyphens because in some browsers I had trouble grabbing the elements via JavaScript if they had a hyphen in the class/id name but that was a long time ago and jquery just seems to work whatever the naming convention.
Comment made at 1:54am, 7th November 2008 (a Friday).
@John Gallagher – That’s called “Camel Case” by most folks…
I use both without problem, so if there’s a reason not to use underscores, I’d like to hear it as well.
Ciao