Tony Benham wrote:
I have been getting to grips with css recently (very slowly), and one area
I have a problem is when to use class selectors or id selectors. Are there
any guidelines when to use each type ? The key difference is, AFAICS, is
that id selectors can only be used once, whereas class selectors can be
used repeatedly ? Is this the main differentiator ?
Well from a technical point of view, yes. But that doesn't mean that if you
only have one you should use id. Think of it this way:
id="foo" => This element is called 'foo'
class="foo" => This element is of type 'foo'
Now just because there may be only one element of type 'foo' on the page, it
doesn't mean that the element's _identity_ is 'foo'. Apart from anything
else, it's a pain to change it all around when you decide to add another
element of that class to a document.
There is also a difference in the way specificity is computed. Selectors
with ids in are more specific than selectors with classes in.
--
Jim Dabell