|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Newbie: class vs id selector?
I'm a CSS newbie and have a basic question.
It seems that the selectors class and id do ecaxtly the same thing. So this begs the question: Which one should I use and why is it better then the other selector? I see that both should not begin with a number (css1) and I am unclear about the requirement for the selector to be unique. Thanks Folks! |
|
#2
|
|||
|
|||
|
you can use either in most situations but you should really use id for unique elements otherwise any js or other code using DOM to access and manipulate the elements will screw up. use class for something of which there may be many.
|
|
#3
|
||||
|
||||
|
the html4.01 specs explains the difference between id and class,
and the uses of these attributes, and why id have to be unique. http://www.w3.org/TR/html401/struct/global.html#h-7.5.2 http://www.w3.org/TR/html401/struct...anchors-with-id |
|
#4
|
||||
|
||||
|
Think of it this way;
A class refers to anything having the characteristic class_name, such as boy, girl, redhead, blond, bald, righthanded or lefthanded. As you can see, a class may have a variety of individuals and an individual may belong to a variety of classes. An id, on the other hand, refers to only one individual and must be unique within the scope (page). Thus there is only one individual named Mary_Jones, and Mary_Jones refers only to that one individual. cheers, gary
__________________
There are those who manage to build a web site without knowing what they're doing; thereby proving to themselves they do, indeed, know what they're doing. My html and css workshop, demos and tutorials. Ask a better question, get a better answer. |
![]() |
| Viewing: Dev Shed Forums > Web Design > CSS Help > Newbie: class vs id selector? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|