XML technologies / CSS / CSS Cookbook / General information / Different selectors for assigning styles / Child selectors

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

A child selector selects an element that must be a direct descendant of its parent. A child selector is represented by a closing angle bracket, often between two type selectors, as shown here:

p > strong {
  text-decoration: underline;
}

In this example, the element strong is underlined only when it is directly inside the p element (see the next figure):

<div>
 In diesem Teil des Satzes passiert gar nichts, weil <strong>strong</strong> kein direkter Nachkomme des div-Elements ist
 <p>Dieses <strong>strong-Element</strong> ist dagegen ein Kind-Element des p-Elements und erhält daher die in der CSS-Regel für diesen Fall festgelegten Stildefinitionen.</p>
</div>

Effect of the child-selector rule

Figure: Effect of the child-selector rule.

The diagram in the following figure shows which elements the CSS rule affects.

The element selected by the child selector within the page structure

Figure: The element selected by the child selector within the page structure.

The first strong element in the two figures is not underlined because it is not inside the p element. The style requires a direct parent-child relationship between the elements. This is a simple but essential difference between a descendant selector and a child selector.

  

 

 

 

back to top