Careeroza
Combinators
Combinator Selectors
| Combinator | Example | Selects |
|---|---|---|
| descendant (space) | div p | all p anywhere inside a div |
child (>) | div > p | p elements that are direct children of a div |
next sibling (+) | h2 + p | the first p right after an h2 |
subsequent siblings (~) | h2 ~ p | every 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