Parents versus ancestors
You’ll hear the terms parent-child and ancestor-descendant. Not all ancestors are parents, although all parents are ancestors. Get it? Probably not. I didn’t get it at first. Few people do. These relationships don’t mean exactly the same thing. A parent-child relationship is the closest possible: The parent is precisely one level (in the tree structure) above its child. However, inheritance can flow further than one level, and when it flows down past a child (to that child’s children or beyond) that relationship is described as an ancestor-descendant relationship.
The shaky tree
Yep. The “tree” metaphor is a little shaky, to be honest. Perhaps it’s quite a bit shaky, given that the tree is upside down if its root is located at the top of the diagram. But don’t be picky. Just thank goodness that those in charge didn’t decide to lay the whole diagram sideways or use an interplanetary metaphor instead.
Back to our attempt to come to grips with this tree structure. Each element on the Web page has its place in the hierarchy, its location within the relationships of parents and children. Each element is either a parent of some other element, or its child, or both. Also, some children are siblings (trees so often have siblings). I get to siblings shortly. Anyway, the parent is the bigger branch nearer the root, and the child is the little branch that shoots off from the parent branch.
One reason to try to visualize the tree structure is to grasp how inheritance works. Styles can be applied to some elements, but not to others, based on the structure of the document. Styles wouldn’t even know how to work if there weren’t a tree structure or something similar.
You can leverage your knowledge of the tree structure to specify various kinds of specialized targets for your styles. What if, for example, you want to underline italics in paragraphs, but never underline them in headlines? You could go through the entire document entering a border-bottom property or a text-decoration: underline for each italics element in paragraphs, but then you aren’t using the efficiency of CSS.
Here’s an example that accomplishes just that by using a technique called contextual selectors. (Recall that a selector is just another name for an HTML element, but it’s called a selector when it’s used to specify a CSS rule.) In the following style, the p and I are both selectors.
Selecting by context rather than grouping
As you doubtless noticed and applauded more than once, I’ve rarely repeated myself in this book. However, because of the similarity between grouping and contextual selectors, I’ll repeat myself here. Don’t get this contextual selectors technique confused with grouping, where you simultaneously define a style for several selectors. If you want to group p and I so that all paragraphs and all italics are underlined, use commas to create a group style, like this.
Here’s yet another shortcut. What if you want to define multiple contextual selectors using the same style? What if, for example, you wanted b strong span’s blue background to also be used when italics appeared with an h2 headline? You don’t have to create a separate style definition, just use a comma to separate the contextual selectors, just as you would when grouping normal selectors
Styling distant descendants
But can you use the universal selector with a contextual selector? Aren’t you the clever kitty! This technique is somewhat confusing, but it can be done. The effect is to apply styles to elements a certain degree of distance from ancestors. For example, if you want only list elements that are great-grandchildren to be red
Warning: This trick also doesn’t work in Internet Explorer. Attribute selectors allow you to define a style that applies only to places where a particular HTML attribute exists as part of an element.
However, this doesn’t work in Internet Explorer, so you can ignore it for now (and perhaps forever). For more on this particular kind of specialized selector.