Re: Difference between class and id

Gabriel wrote:
> Hello:
> 
> It's just a little question that's been around because I just discovered the
> power of <div>.
> What's the difference between using <div id=smth> and <div class=smth>
> (and <div id=smth1 class=smth2>)?

The meaning.

    <div id="smth1">

...means that the <div> is *the* "smth1". For example:

    <div id="footer">

...or

    <div id="monday-schedule">

However,

    <div class="smth2">

...means that the <div> is *a* "smth2". For example:

    <div class="section">

...or

    <div class="schedule">

...or

    <div class="important citation">

This last one means that the <div> is important, and is a citation. (The class 
attribute takes a space separate list of words.)

So the "id" attribute is an identifier unique to the document, while the class 
attribute merely classifies the element.

Note that using <div> and <span> should generally be avoided if at all possible. 
The elements are structurally meaningless. It is better to use <p>aragaphs and 
<h1>eaders, etc. See the HTML specification [1] for more details.

(In object-orientated terms, the class attribute represents a kind of "isa" 
inheritance, while the id attribute represents a kind of instance reference.)

[1] http://www.w3.org/TR/html4

-- 
Ian Hickson
``The inability of a user agent to implement part of this specification due to
the limitations of a particular device (e.g., non interactive user agents will
probably not implement dynamic pseudo-classes because they make no sense
without interactivity) does not imply non-conformance.'' -- Selectors, Sec13

Received on Monday, 27 May 2002 11:27:03 UTC