No Mod Required

Question: When is a CSS Class not a CSS Class?

Answer: When it's a unique identifier.

Check out this class attribute generate by my beloved Wordpress' upload feature:

class="alignleft size-medium wp-image-4590"

See anything suspicious? I sure do. wp-image-4590 is a unique identifier being passed off as a class. Why? I actually have no clue as I'm not privy to the thought process behind that particular piece of code :) What I do know is pretty much demands to be an ID. When I teach this stuff to people, I say "If it's unique, meaning there will ever only be one of them, make it an ID. If there's more than one or it's a general descriptor, make it a class." So I look at that code block every time I upload an image and I frown. Then I blow away the whole class attribute away, since I use none of them.

This is just splitting hairs, I know. Using a class like that is basically harmless. But, truth be told, splitting hairs helps me solidify my ideas about the way these things should work. That, in turn helps me improve the way my crew and I do our thing. So? Hairs I split and everyone is happier.

I'm just doing my small part to make the web a better place one nitpicky, semantic post at a time.

What Other People Are Saying

Want to join in the discussion? Leave a comment using the form below or link to http://www.drunkenfist.com/304/2008/06/01/question-when-is-a-class-not-a-class/ from your own site to have your post show up here.

2 Responses to “Question: When is a CSS Class not a CSS Class?”

  1. Tom Says:

    The one thing I can think of is that they are anticipating that the image might be used more than once on a page. In which case, an ID wouldn't work.

    Logic could be inserted to decide if the element only appears once on a page or not and give an ID or class accordingly, but for simplicity's sake, it makes more sense to keep it as a class.

  2. rob Says:

    I guess the question is, how often is one image inserted into a Wordpress post multiple times? I bet it doesn't happen all that often. In which case WP is coding for a problem that isn't all that much of a problem.

    Wordpress isn't the only one guilty of this, of course, Drupal used to (and might still) have unique class names for all of their menu items-


    <li class="last menu-1-2-2">

    I guess the same argument could me made there, although I liked it even less in Drupal :)

Leave a Reply

Note: Wrap all of your code blocks in <code>...</code> and replace < and > with &lt; and &gt;, respectively.