[thelist] CSS id or class?

aardvark roselli at earthlink.net
Thu Aug 1 10:11:01 CDT 2002


> From: David.Cantrell at Gunter.AF.mil
[...]
> My question is, when is it more appropriate to use ID vs class, and
> vice-versa?
>
> I would think ID is supposed to be unique in the document, though I
> may be wrong here. I used to use IDs a lot more, but for the past
> couple years have used class almost exclusively. I know ID comes into
> play with DHTML, so you may want to make sections of your code (nav
> links, etc) available for future scripting, but is there any other
> reason for choosing ID over class or the other way around?

i'll always go with id if i know the thing i wanna do is unique... for
instance, if i have a navbar, i'll id that as NavBar... my footer gets
Footer...

but i may want to style the content in my content area, and it turns
out i may wish to reuse that style elsewhere, even though i only
have one 'content' area... for instance, it might be perfect for a little
box on the side of the page, or i might need to reapply it in nested
elements because NN4.x drops the style...  so i'll use a class of
Content instead of an id...

sometimes i find a style could fit in more than one place, so i might
go back and and change it from an id to a class...

sometimes i find myself styling something so specifically for one
area that i change it from a class to an id...

sometimes i break the general part of that into a class, and the real
specific stuff into an id...

there are no hard rules -- it's all about doing what you can to make
it easy to maintain and understand, while minimizing code bloat,
room for errors, and wacky support...

> What scenarios call for either or both?

lots...

you could conceivably use classes in place of all ids, but you
couldn't use ids in place of all classes..

--
Read the evolt.org case study
Usability: The Site Speaks for Itself
http://amazon.com/exec/obidos/ASIN/1904151035/evoltorg02-20
ISBN: 1904151035



More information about the thelist mailing list