1. Computing & Technology

Discuss in my forum

What is a CSS Child Selector?

By , About.com Guide

A CSS child selector applies to the elements that are children of another element. A child element is an element that is the immediate or direct descendant of another element. For example all the <li> elements in an unordered list are children of the <ul>. But any anchors on the <li> elements are not children of the <ul>:

<ul>
<li><a href="">...</a></li>
</ul>

Define child selectors by using two type selectors separated by a greater-than sign (>).

li > a {
text-decoration : none ;
}

If you have the following CSS:

p > strong {
color : purple ;
}

Think about how it would impact the following HTML:

<div>Text in a div with <strong>a strong</strong> element inside of it.</div>
<p>HTML in a paragraph with <strong>a strong</strong> element inside of it.</div>

Only the second strong element will be purple.

©2012 About.com. All rights reserved.

A part of The New York Times Company.