Classes (and IDs) aren’t owned by CSS

While I was doing some research for a new talk I am preparing, I just found out this explanation written by Jeremy Keith, in response to a comment to one of his articles on A List Apart, dating back to June 2006:

The class attribute exists to add semantic meaning. It can be repurposed by CSS. It can also be repurposed by JavaScript and the DOM. Neither use is more correct than the other. Both are valid. Both are building on top of the inherit semantic value of the attribute.

It’s not that I “use the attribute class as a JavaScript hook”, I *repurpose* the attribute class (or ID) as a JavaScript hook… or a CSS hook, depending on whether I’m trying to enhance behaviour or presentation.

Classes (and IDs) aren’t owned by CSS. Neither are they owned by JavaScript—they belong to the markup. But both CSS and JavaScript can legitimately use them as hooks.

I understand your point that one shouldn’t add *unnecessary* classes or IDs just for the sake of JavaScript, but that’s equally true of CSS: one shouldn’t (theoretically) add classes or IDs just to act as hooks for presentation (for the same reasons you cite).

Below a screenshot of the full comment, for future reference:

Is this idea something we have lost along the way? Today it’s forbidden to use IDs to apply styles in CSS and we are supposed to use only class names for styling purposes.

Have we been maybe a little too overzealous in defining what goes into CSS and what absolutely can’t?

Personally, I continue to think that:

One of my primary axioms is: Everything is best for something and worst for something else. The trick is knowing what is what, for what, when, for whom, where, and most importantly, why. – Bill Buxton, 2007

Go to blog index