1

Possible Duplicate:
Cascading style sheets use “id” or “class”

When should i use an id v/s class in my css file.

I know that id is used when it is a unique element.

For all practical purposes if I assume that none of the elements are repeating what should I use?

Community
  • 1
  • 1
user544079
  • 16,109
  • 42
  • 115
  • 171
  • Semantics. "assume that none of the elements are repeating" isn't valid. You have to make a clear definition of what can be repeated and what cannot be repeated. Making more-or-less arbitrary assumptions reduces the semantic content of your styles. Why eliminate the semantic meaning? – S.Lott Apr 25 '11 at 22:50
  • 3
    this question is a dupe http://stackoverflow.com/search?q=class+vs+id – RedDeckWins Apr 25 '11 at 22:51
  • 2
    I wish we could vote to close as a dupe of a certain search term. You know, just to tell people "we have a search function". – BoltClock Apr 25 '11 at 22:52

3 Answers3

1

The design decision comes from practical use. As I'm sure you're guessing: yes, it is perfectly standards-valid to use classes for everything. If you ever need to manipulate elements (take a look at jQuery), though, it is much easier to work with an id rather than a group of elements in a class.

You seem to understand: ids for unique elements, classes for multiple elements with the same design/purpose.

Max Vu
  • 464
  • 2
  • 10
1

A good heuristic approach to think about is that the id is like the name of the element for if you want to refer to it specifically and the class is for properties of the element that some other elements around your site might have also.

Another thing to think about is specificity, which is the rules for when one css rule overrides another. Here is a good read for that: http://css-tricks.com/specifics-on-css-specificity/

B_.
  • 2,164
  • 2
  • 17
  • 20
1

ID has a higher selector specificity than a class. This means that if you have a class and id competing for properties, the id css properties will most likely overwrite the class properties. If I am correct, an ID is worth 100 points, a class selector is 10 points, tag selector is 1 point, and an in-line style is 1000 points. The higher the amount of points, the higher specificity and the higher it has a chance to overwrite the other selector properties.

Hope this helps.

rabid_zombie
  • 982
  • 2
  • 16
  • 32