W3C home > Mailing lists > Public > www-style@w3.org > May 2002

Re: Difference between class and id

From: Shasta k Willson <shasta@cs.uoregon.edu>
Date: Tue, 28 May 2002 12:08:43 -0400 (EDT)
Message-ID: <B918F894.2042%shasta@cs.uoregon.edu>
CC: <hwg-style@hwg.org>, <www-style@w3.org>

>> What's the difference between using <div id=smth> and <div class=smth>
>> (and <div id=smth1 class=smth2>)?
>> Are they supposed to have different content?
> 
> Basically, you can only have one ID of a certain value per page, but you
> can reuse classes all you want.

I think this answer is basically correct, but incomplete.  I believe that ID
is more or less an identifier for the Object.  This allows it to be
referenced in Javascript and (and thus DHTML) as well as allowing you to
specify CSS attributes for it.  CLASS on the other hand is just CSS and is
used when a whole group of something needs that set of attributes.  Note
that this is opposite of the comment someone posted about class/javascript.
I think the confusion is that Java uses Classes to define Objects.  Those
classes and CSS classes bear little to no resemblance.  JavaScript is Object
oriented too but we don't really see the classes, and it has nothing to do
with Java.  (Name notwithstanding, it was a marketing ploy by Netscape - the
only thing they have in common is being Object Oriented, like many other
languages.)

I might use class/id like this:

>a href... class="navbar"<
>a href... class="navbar" id="current"<

Where maybe current makes the background grey or something so that my
current page link is visually different in the navigation bar.  (See
www.sallysheklow.com for a work in progress.)

Unfortunately ID is not well supported in NN4.x  (I think it's ok
elsewhere.)  So instead I usually avoid it for CSS (Still need it for
JavaScript) entirely and do this:

>a href... class="navbar"<
>a href... class="current"<


I'm pretty sure this isn't an elegant explanation.  I did once see a good
explanation, but can't remember if it was on Eric Meyer's site, or on
webmonkey.com

- Shasta Willson
Received on Tuesday, 28 May 2002 14:12:33 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:54:14 GMT