2 replies [Last post]
patrickco
patrickco's picture
Offline
Enthusiast
Los Angeles
Last seen: 11 years 16 weeks ago
Los Angeles
Timezone: GMT-7
Joined: 2005-04-17
Posts: 157
Points: 12

Hi everyone,

One thing that has always eluded me, is when should one use an id over a class?

The only difference I have ever been able to find is that id's can only be used once on a page... But if that's truly the only difference, why ever use an ID? Why not always use classes, since it makes markup easier down the road if you realize later you want to reuse something?

-patrick

Tags:
Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 7 years 38 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

IDs have higher specificity

IDs have higher specificity so in the case of something that is nested, your CSS rule may be shorter by using an ID than a class, e.g.

p#different { }

instead of

.rule1 .rule2 .rule3 p.different { }

Also, accessing elements via javascript is different depending on whether it's a class or ID.

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

thepineapplehead
thepineapplehead's picture
Offline
Moderator
Last seen: 10 weeks 3 days ago
Timezone: GMT+1
Joined: 2004-06-30
Posts: 9683
Points: 819

http://www.csscreator.com/nod

http://www.csscreator.com/node/7113

But it probably would be a good idea to modify the article to explain the "why" Laughing out loud

Verschwindende wrote:
  • CSS doesn't make pies