Combinators

Combinator Selectors

CombinatorExampleSelects
descendant (space)div pall p anywhere inside a div
child (>)div > pp elements that are direct children of a div
next sibling (+)h2 + pthe first p right after an h2
subsequent siblings (~)h2 ~ pevery p after an h2 with the same parent

Example

Example

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    .box p { color: blue; }          /* descendant */
    .box > p { font-weight: bold; }   /* direct child only */
    h2 + p { background: #fff3bf; }   /* paragraph right after h2 */
  </style>
</head>
<body>

<div class="box">
  <p>Child paragraph: blue and bold</p>
  <section><p>Grandchild: blue, not bold</p></section>
</div>
<h2>Heading</h2>
<p>Right after the h2: highlighted</p>
<p>Not directly after: plain</p>

</body>
</html>

Result