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

Re: Difference between class and id

From: Ian Hickson <ian@hixie.ch>
Date: Mon, 27 May 2002 16:26:55 +0100
Message-ID: <3CF2503F.2060402@hixie.ch>
To: Gabriel <ghrivera@sinectis.com.ar>
CC: hwg-style@hwg.org, www-style@w3.org
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 GMT

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