Picking the parts - CSS selectorsOk so we know we can use the elements type (e.g
body)
id or
class to select what we want to style. Ok that great, but I need something a bit more than that. We it your luckily day, there is a lot more you can do. Lets start with a real life example.
Ok so I got hundreds of web pages and they have links all over them, but I only want to apply a style to the links in the menu which has the
id of
menu. How can I do this with out adding a class to ever link on every web page? The solution is quite simple, and it has the wacky name of the
descendant combinator, it looks like this.
#menu a {
border : 1px dotted #990000;
}
You can see what I have done here. First I have selected the menu by using its
id, then I have selected
a this results in the style sheet finding the menu and searching for any links in it and applying the styles to those elements.
This is very well and good, but is effect all links in the menu, and you have adds in it too, and you don't want them to be effected my the CSS rule. This is when the
child combinator comes in to play.
#menu > a {
font-weight : bolder;
}
This is much the same as the previous example, just we have added a
> this signals that the element is this case a link,
a must be directly inside the menu that we have given the
id of
#menu.
There is also another CSS selector method, and one that sadly is very rarely ever used because it is not supported by IE.
h2 + p {
font-weight : bolder;
}
This rule would make the first paragraph directly after a
h2 heading bold. This would be very useful, but as I said IE does not support it.
Edited by Michael, 01 December 2006 - 01:46 AM.