Child selector in CSS

The child selector > allows you to select elements by direct nesting within each other.

Syntax

cssselector1 > selector2 {
  
}

Example

Let's select all the b tags that are directly inside the p tags and color them red:

html<p>
  text <b>+</b>
</p>
<p>
  text <i><b>-</b></i>
</p>
cssp > b {
  color: red;
}

Результат:

Example

Let's select all b tags directly inside elements with the class .block and color them red:

html<p class="block">
  text <b>+</b>
</p>
<p class="block">
  text <i><b>-</b></i>
</p>
<p>
  text <b>-</b>
</p>
css.block > b {
  color: red;
}

Результат:

Example

Let's select all elements with the class .elem that are directly inside elements with the class .block and color them red:

html<p class="block">
  text <span class="elem">+</span>
</p>
<p class="block">
  text <i><span class="elem">-</span></i>
</p>
<p class="block">
  text <span>-</span>
</p>
<p>
  text <span class="elem">-</span>
</p>
css.block > .elem {
  color: red;
}

Результат:

See also