> Here's a general question about CSS that I've been able to quite figure out.
> If one has a DIV that will only appear once per page, which should one use:
> a class or an id?
> Classes are obviously useful at applying to multiple elements, or applying
> multiple classes to one element. What advantages have that classes do not?
> Specificity comes to mind, but the CSS rules regarding this are hazy in my
> mind and I do not know if ids provide more specificity than classes.

If the element is only available once on the page that either means it
is a special case or it is an important element of the overall site
layout.

This warrants an ID rather than a class, for several reasons:

1) A designer who will look at your CSS and XHTML and needs to
maintain it realises immediately that this element can be there once
and is something unique
2) The specificity is higher than a div with a class
3) It is shorter to write child selectors:
#foo p a vs. div.foo p a (yes you could argue that div#foo would be
needed, but the higher specificity of an ID warrants the ommission)
4) Scripts could reach your DIV via getElementById rather than using
an extra method and looping to find the DIV with the className

Classes on the other hand give a CSS-savvy user the information that
the element is part of a collection and can appear several times.

--
Chris Heilmann
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
Binaries: http://www.onlinetools.org/
______________________________________________________________________
css-discuss [EMAIL PROTECTED]
http://www.css-discuss.org/mailman/listinfo/css-d
List wiki/FAQ -- http://css-discuss.incutio.com/
Supported by evolt.org -- http://www.evolt.org/help_support_evolt/

Reply via email to