Using ids in CSS selectors makes it possible to very directly target elements that are decendant of the element that has the id. Typically, a page would be divided up into a number of divs, all representing specific sections of the page, like "header", "menu", "content", "footer", etc.
Giving each div an id, using these ids in CSS selectors you can specifically target elements that are in these divs.
I cooked up a simple example to illustrate this:
CSS:
Code:
#special {
border: 1px dashed #999;
}
#special p {
color: #c00;
}
.normal {
color: #090;
}
p {
color: #00c;
}
HTML:
Code:
<div id="special">
<p class="normal">Is this text green?</p>
<p>This text should be red, anyway</p>
</div>
<div>
<p class="normal">This text is definitely green</p>
<p>This text is blue, with a <span class="normal">green bit</span> I guess</p>
</div>
The text color of the first paragraph in the "special" div is not green but red, since the "#special p" selector overrides the ".normal" class selector; the second p is red because of that same "#special p" selector.
In the second div, the first paragraph is green because of the class; the second p is blue, apart from the spanned bit which has the same (green) class as the first p.
Bookmarks