classes vs IDs - Page 2

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: classes vs IDs

Joshua Beall wrote:
Quoted text here. Click to load it

Others have discussed the rules. But I didn't see an answer to your question
about the advantage.

My main reason for using ID is that it is highly "specific". This means
(roughly) that where an element has conflicting CSS rules that could be
applied to it, a rule selected by its ID will take precedence. Easier to
control & predict what will happen in complex cases.

Another reason is that it is more obvious which are intended to be the one-off
page elements, and which are intended to be many-of. This is typically for
clarity of understanding rather than for anything functional.

Barry Pearson / / /

Re: classes vs IDs

On Thu, 08 Apr 2004 00:39:47 GMT, "Joshua Beall"

Quoted text here. Click to load it

Stronger than "intended". It is invalid to use the same id value more
than once in a document.

Probably nothing bad will happen. But it may break some DHTML.

Because id is unique, it can be used to transform from knowing the id
value to having a reference to a DOM element. You can't do this with
class, at best you can get a possibly many-membered set of elements.

Given the element, you can of course get either id or class values.

Quoted text here. Click to load it

There are very few cases (in a CSS context) where it is useful to use
id rather than class. There are few cases (in a DHTML context) where
it is useful to use class rather than id. However the two don't
conflict and they sometimes have a synergistic benefit, so it's useful
to have both available.

As others have posted, id has a higher priority in CSS selectors (or
"specificity" as it's termed)  than class does, so their combination
is often useful.

A couple of digressions:

CSS 2 allows multiple classes in a selector,
CSS 1 didn't

You can also apply multiple class values to a HTML element
simultaneously, by placing all the values in a single class attribute.
Browser support is however, near useless !

"id" is different from "ID".  "ID" is a type in a DTD, meaning "this
attribute has behaviour as a unique identifying property". "id" is
just the name of an attribute and doesn't (of itself) imply anything
about the behaviour.

In HTML, "id" is defined to be of type "ID", so they end up as meaning
the same thing. This isn't the case for XML documents though, unless
you specify it in their DTD.  You can also define an attribute called
"id" that's no relation to HTML's and give it different behaviour
(this will confuse people though), or (more usefully)

I'm currently working with an XML schema that's horribly familiar to
at least one other newgroup regular.  It has two elements (and others)
with a "tileref" attribute. One is of type ID, the other of type
IDREF.  This is a reasonable way to work an XML schema (XML knows
which is which), but for the sake of humans reading it, then please
use different names for the attributes, i.e. "tile_id" and "tile_ref"
would have been much better.

Smert' spamionam

Site Timeline