By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Id v Class Selectors

P: n/a
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 ?

Regards
Tony B

Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.