View Full Version : CSS identifiers

10-20-2009, 05:30 PM
Hi, is there a benefit to writing code like:

div#wrapper {

#wrapper {

10-20-2009, 05:59 PM
Yes sure there is a difference and/or benefit depending on your need.

the first rules on;y applies to divs that have their id as wrapper while the second rules applies to any html object that has its id wrapper.

10-20-2009, 06:22 PM

What about id vs class? What is the benefit of using id if you can only use each id once? What justifies the existence of the id parameter other than organization?

10-20-2009, 06:30 PM
IDs can be used as anchors for internal links:

<a href="#headline">link</a>

<h2 id="headline">Headline</a>

and as simple handle for JavaScript/DOM manipulation.

Also IDs have a higher CSS specificity than classes so when you write

.classname {background-color: red;}
.classname {background-color: green;}

the element with the respective class will have a green background while if you have this:

#idname {background-color: red;}
.classname {background-color: green;}

the background color will still be red (provided the styles address the same element).

Have a look at http://htmldog.com/guides/cssadvanced/specificity/ and http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

10-20-2009, 06:36 PM

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum