4

Possible Duplicate:
CSS Best Practice about ID and Class?

To me, it seems like both achieve the same thing. So are there any rules or standards as to when ids are more appropriate over classes and vice versa.

I know this question may be seen as subjective, I don't have any preference over one or the other and am not trying to peddle anything here. I am genuinely curious as I am new to front end web development and would like to know which to use and when.

Community
  • 1
  • 1
  • 3
    From the related questions (also linked in the list which popped up while you entered this question ...): http://stackoverflow.com/questions/298607/css-best-practice-about-id-and-class – BalusC Feb 27 '10 at 01:04

3 Answers3

3

CSS Best Practice about ID and Class?

Community
  • 1
  • 1
ScottE
  • 21,530
  • 18
  • 94
  • 131
1

When you need give multiple things an attribute/style, use a class, if it's singular, use an ID. If it's something I want to readily identify, like LoginBtn then it's an ID, if it's a style, like say an anchor that's blueLink, it's a class.

Another consideration for a lot of developers is javascript. e.g. a jQuery selector $('#id') is much faster than $('.class'), so if you're only dealing with one element, this is also an advantage.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • and a class named `blueLink` is not a good class, classes should not say **how** they style, but **what** they style. Ftw, I know that is just a example. :) – jonathancardoso Sep 26 '11 at 23:03
0

This is not subjective. Classes can be used to give the two separate entities in the html (such as two divs) the same styling. Id's are unique and therefore can only style 1 element at a time.

edit: clarification

RedDeckWins
  • 2,111
  • 14
  • 16