Class and ID are not presentation-centered
Wednesday / 30 March 05
Today’s web design encourages the separation of presentation and structure. XHTML and the power of CSS encourage this approach to authoring your documents. Yet, I often bump into someone who claims that this is not entirely possible. The class and ID attribute available in HTML is all about presentation — or aren’t they?
I thought about forming a very family-unfriendly reply noting the theoretical nitpicking, but figured it wouldn’t be really adding anything to the discussion. Instead, I’ll give you the W3C and a pinch of me.
Semantics
Even though the class and ID attributes are commonly used in conjunction with CSS, this doesn’t mean one defines another. The presentation might spawn a need for a class or ID, but that’s simply because of the way we think. We start with a bare-bone document and add classes, IDs and groupings when deemed necessary. Of course, the other way around is perfectly possible as well.
Furthermore, class and ID attributes should be carrying semantically correct names. Names such as “left” to indicate something aligned left isn’t what we are aiming for. Instead, you might consider calling it “illustration” — even though in reality it will only be floated to the left.
Multi-purpose
As the W3C explains, classes and IDs aren’t only used in conjunction with CSS. Besides using them to style elements, they are a key element in unobtrusive Javascript to hook into the DOM. No more Javascript calls in your mark-up. No more onmouseover attributes in your mark-up. Unobtrusive Javascript separates the behaviour from your structure, leaving us with three levels living their own life, yet holding each others hand.
IDs are used to describe relationships between two elements as well. A well known example would be defining a relationship between a form label and input element using a label’s for attribute.
A third example is the fact that any ID attached to an element functions as an anchor inside the document as well.
Class and ID are not presentation-centered
Really, they are not. They are just used often in a presentation related environment. They can do so much more and mean so much more for your average document. Perhaps they can even do a back-flip, if you ask them nicely.

1Dominic posted:
30 March 05, 08:27:38 AM
Hi Jeroen, nice article.
Surely it is obvious that Class and ID cannot be possible, simply by the fact that you taught me about DIVs, etc - the HTML that you write is not about presentation - that's what the CSS is for.
Please correct me if I'm wrong :)
Take care, Dominic