Descendant Selectors


Descendant selectors are used to select elements that are descendants of another element.

For example, in the HTML sample shown in Listing 3.1, three <a> elements are descendants of the <li> elements. To target these three <a> elements only, and not all other <a> elements, a descendant selector can be used as shown in Listing 3.6. This selector targets any <a> element that is nested inside an <li> element.

Listing 3.6. CSS Code Containing Descendant Selector
li a {      color: green; } 

Descendant selectors do not have to use direct descendant elements. For example, the <a> element is a descendant of <div > as well as the <li> element. This means that #nav a can be used as a selector as well (see Listing 3.7).

Listing 3.7. CSS Code Containing Descendant Selector
#nav a {      color: red; } 

Descendant selectors also can include multiple levels of descendants to be more specific as shown in Listing 3.8.

Listing 3.8. CSS Code Containing Descendant Selector
#nav ul li a {      color: green; } 




Sams Teach Yourself CSS in 10 Minutes
Sams Teach Yourself CSS in 10 Minutes
ISBN: 0672327457
EAN: 2147483647
Year: 2005
Pages: 234
Authors: Russ Weakley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net