JeroenMulder.com, Skip Navigation


Current Location: / Weblog

Recent Entries

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.

Comments

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

2Dominic posted:

31 March 05, 06:25:22 AM

Excuse me, I meant to say:

Surely it is obvious that Class and ID cannot be ***presentation-centred***.

Dom

3John A posted:

31 March 05, 19:49:47 PM

I agree 100% here. Class and ID do not imply any type of presentation. They are simply hooks on which to apply the presentation...

4Lachlan Hunt posted:

01 April 05, 05:22:26 AM

I absolutely agree, and the W3C QA team have been promoting this exact message for a while with their article: use class with semantics in mind.

Post Your Comment






Comment Guidelines:
Please keep your comments relevant. Abusive, inappropriate and anonymous may be edited or removed, if deemed necessary.

Privacy Note:
Your e-mail address will never be displayed in public or forwarded to third-party organisations and is only stored for security reasons.


Recent Entries

Blogging Friends