In CSS you can pinpoint elements depending on their ancestors, their parent, or their siblings (see page 31).
An ancestor is any element that contains the desired element (the descendant), regardless of the number of generations that separate them.
Figure 9.12. I've snipped the text to make the relationships between elements easier to see. Each indentation represents a generation. Note that there are two second generation p elements, directly within the gaudi div, and one third generation p element, within the works divs (within the gaudi div).
To select elements to format based on their ancestor:
Type ancestor, where ancestor is the name of the element that contains the element you wish to format.
Type a space (Figure 9.13).
Figure 9.13. The space between div#gaudi and p means that this selector will find any p element that is a descendant of the gaudi div, regardless of its generation.
If necessary, repeat steps 12 for each successive generation of ancestors.
Type descendant, where descendant is the name of the element you wish to format.
Figure 9.14. All of the p elements that are contained within the gaudi div are red, even if they're also within other elements within that gaudi div.
A selector based on an element's ancestor is called a descendant selector.
Descendant selectors are well supported by current browsers.
Don't be thrown off by the div#gaudi portion of the example (even though it's ugly). Remember that it simply means "the div whose id is equal to gaudi" (see page 140). So div#gaudi p means "any p element that is contained in the div whose id is equal to gaudi".
A parent is the element that directly contains the desired element (the child) with no intermediate containing elements.
To select elements to format based on their parent:
Type parent, where parent is the name of the element that directly contains the element you wish to format.
Type > (the greater than sign) (Figure 9.15).
Figure 9.15. This selector will only choose those p elements that are children of the gaudi div. They may not be contained within any other element in order to qualify.
If necessary, repeat steps 12 for each successive generation of parents.
Type child, where child is the name of the element you wish to format.
Figure 9.16. Only the first two p elements are children of the gaudi div. The two other p elements are children of the works div. For the (X)HTML code used in this example, see Figure 9.12 on page 141.
It's sometimes useful to be able to select only the first child of an element, as opposed to all of the children of an element.
To select elements to format that are the first child of their parent:
Type parent, where parent is the selector for the desired element's parent.
Type :first-child (just like that) (Figure 9.18).
Figure 9.18. This selector chooses only the p element that is the first child of the gaudi div.
An adjacent sibling is the element that directly precedes the desired element within the same parent element.
Figure 9.19. The first p element contained in the gaudi div is red. The second one isn't.
To select elements to format based on an adjacent sibling:
Type sibling, where sibling is the selector for the element that directly precedes the desired element within the same parent element.
Type + (a plus sign).
If necessary, repeat steps 12 for each successive sibling.
Type tag, where tag is the name of the element you wish to format.
Figure 9.20. This selector chooses only those p elements which directly follow a sibling p element.
Also see Parents and Children on page 31.
Figure 9.21. Only the p elements that directly follow a sibling p element are red. This selector would be useful for indenting all paragraphs except the first.
The :first-child part of the selector is called a pseudo-class, because it identifies a group of elements without you, the designer, having to mark them in the (X)HTML code. It is supported by all major current browsers (including IE starting with version 7).
Adjacent sibling selectors are currently supported by Firefox, Opera, and IE 7.