1. Computing & Technology

Discuss in my forum

What is a CSS Descendant Selector?

By , About.com Guide

A CSS descendant selector applies to the elements that are inside another element. For example an unordered list has a <ul> tag with <li> tags as descendants. In the following HTML:

<ul>
<li><a href="">this is a link</a></li>
</ul>

The LI tags are descendants of the UL tag. The A tag is a descendant of both the LI (child descendant) and UL (grandchild descendant) tags.

Define descendant selectors by using two type selectors separated by spaces.

li a {
  text-decoration: none ;
}

Remember that it doesn't matter how many tags are in between the two tags. If the second element is enclosed anywhere within the first element it will be selected as a descendant.

If you want to select all anchors that are descended from ul elements, you would write:

ul a {
  text-decoration: none ;
}

Also: the selectors that you're descending from don't have to be type descendants. For example, you could set up a descendant selector off of an ID:

#navigation ul {
  background-color: #ccc ;
}

©2012 About.com. All rights reserved.

A part of The New York Times Company.