XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Selectors and CSS rule structure / Selector for descendants
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Recall that in an HTML document’s inheritance hierarchy, every HTML element except html is a descendant of at least one other HTML element. A descendant selector (English: descendant selector) lets you assign style rules that apply only to elements below a particular element in the inheritance chain.
A descendant selector, as in the following example, restricts a style rule to descendants of other elements. Its scope is determined by the HTML elements listed in the rule, separated by spaces and read from left to right.
li em {
color: green;
}This style rule gives green text to all words inside tags <em></em>. However, it applies only when em is a descendant of a list item.
In the following example, the text inside the first tags <em></em> is green, but the text inside the second tags <em></em> is not, because the element em is not a descendant of an element li.
<ul>
<li>Erster Eintrag</li>
<li><em>Zweiter</em>Eintrag</li>
</ul>
<p>Ein Wort in <em>Kursivschrift</em>.</p>With descendant selectors, inheritance need not occur between immediate parent and child elements; it can span several levels.
<div class="seitenleiste">
<p>Falls Sie Fragen haben, <a href="contact.html">rufen Sie unser Büro während der Geschäftszeiten an</a>.</p>
</div>The following style rule therefore also applies to the element a, which is an immediate child of the element p but a second-generation descendant of the element div:
div a {
font-style: italic;
}
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg
back to top