Re: Difference between class and id

>> 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 UTC