CSS Relationship Selectors: Combinators

Master the art of targeting HTML elements with precision using descendant selectors and relationship combinators (`>`, `+`, `~`) in CSS.

Lesson ProgressStep 1 of 9

A Title

A direct child paragraph.

A grandchild paragraph.

Another direct child paragraph.

0 EXP

Welcome! CSS isn't just about single tags. It's about relationships. Let's learn how to style elements based on where they live.

/* CSS Selectors are like a GPS */
/* Let's find our targets */

Descendant Selector (space)

This is the most common and broadest combinator. It's represented by a **space**. The selector article p will select *any* <p> element that is nested inside an <article>, no matter how many levels deep.

/* This selects p.child and p.grandchild */
article p {
  color: blue;
}
<article>
  <p class="child">I am blue.</p>
  <div>
    <p class="grandchild">I am also blue.</p>
  </div>
</article>

It's powerful but can be overly broad, leading to specificity conflicts if not used carefully.

System Check

Which selector will target *all* <a> tags inside elements with a class of .main-nav?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Combinator Captain

Use all four combinator types in the challenges.

🎯
Specificity Sniper

Correctly target a nested element using the child combinator.

🤝
Sibling Selector

Master the `+` and `~` combinators to style adjacent elements.

Mission: Precision Targeting

Using the HTML structure provided, complete the CSS to:

  • Make only the direct li children of nav bold.
  • Make only the p immediately after the h2 red.

HTML Structure


<nav>
  <li>Top Item</li> <li>
    <ul>
      <li>Sub Item</li>
    </ul>
  </li>
</nav>

<h2>A heading</h2>
<p>This paragraph</p> <p>Not this one</p>
  

Your CSS

A.D.A. Feedback:

> Awaiting input...

Challenge: Match the Combinator

Drag the correct combinator type (on the left) to its matching description (on the right).

Adjacent Sibling (+)
Descendant Selector ( )
General Sibling (~)
Child Combinator (>)
Targets any `p` inside a `div`, no matter how deep.
Targets only a `p` that is a direct child of a `div`.
Targets only the `p` immediately following an `h1`.
Targets all `p` siblings that come after an `h1`.

Challenge: Complete the Selectors

Fill in the missing combinators to complete the CSS rules.

.menuli { ... } /* Direct children */
h2p { ... } /* Next sibling */
h2p { ... } /* All following siblings */

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Precision Targeting" project for feedback from other Net-Runners.

The Art of Precision: An Exhaustive Guide to CSS Combinators

Writing CSS is easy. Writing *maintainable* CSS is hard. The secret often lies in your choice of selectors. While class selectors like .button are your daily workhorse, **CSS Combinators** are your precision tools. They allow youto select elements based on their specific relationship to other elements in the Document Object Model (DOM).

Mastering these four combinators is the difference between fighting your stylesheet with !important and creating a clean, predictable, and scalable system.

1. The Descendant Selector (space)

This is the most common and most broad combinator. It is represented by a single space between two selectors.

  • Selector: article p
  • What it means: Selects any <p> element that is a descendant of an <article> element, no matter how deeply nested it is.
  • Use Case: Great for setting general styles within a specific context. For example, ensuring all paragraphs within a blog post have a certain `line-height`.

Example:

<article>
  <p>This is selected.</p>
  <div>
    <p>This is ALSO selected.</p>
  </div>
</article>
<p>This is NOT selected.</p>

The selector article p will target both the child and grandchild paragraphs.

Warning: Be careful! Overusing this selector can lead to high specificity and unintended side effects. .sidebar ul li a is very specific and hard to override later.

2. The Child Combinator (>)

This is your first precision tool. It is represented by the "greater than" symbol.

  • Selector: article > p
  • What it means: Selects any <p> element that is a **direct child** of an <article> element. It will *not* select grandchildren.
  • Use Case: Perfect for navigation menus (.nav > li) to style only the top-level items, not the items in sub-menus.

Example:

<article>
  <p>This is selected.</p>
  <div>
    <p>This is NOT selected.</p>
  </div>
</article>

The selector article > p will *only* target the first paragraph.

3. The Adjacent Sibling Combinator (+)

Now we move to elements on the same level (siblings). This selector is represented by the "plus" symbol.

  • Selector: h1 + p
  • What it means: Selects the *one* <p> element that comes **immediately after** an <h1> element, *if* they share the same parent.
  • Use Case: Styling a "sub-heading" or "intro paragraph" that always follows a main title. Also used for styling labels for form inputs: input:checked + label.

Example:

<h1>My Title</h1>
<p>This is selected.</p>
<p>This is NOT selected.</p>
<div>...</div>
<p>This is NOT selected.</p>

The selector h1 + p will *only* target the first paragraph.

4. The General Sibling Combinator (~)

The more flexible sibling selector. It is represented by the "tilde" symbol.

  • Selector: h1 ~ p
  • What it means: Selects **all** <p> elements that come **after** (not necessarily immediately) an <h1> element, *if* they share the same parent.
  • Use Case: Styling all paragraphs in a section after the section's title, but *not* paragraphs before it.

Example:

<p>This is NOT selected.</p>
<h1>My Title</h1>
<p>This is selected.</p>
<p>This is ALSO selected.</p>
<div>...</div>
<p>This is ALSO selected.</p>

The selector h1 ~ p will target all three paragraphs that come *after* the `h1`.

Specificity & Performance

It's important to know how these affect your stylesheet's complexity.

  • Specificity: Combinators themselves (>, +, ~) add **zero specificity**. The specificity of a rule like div > p is calculated by adding the specificity of div (a type selector) and p (a type selector). However, a rule like div p is *more likely* to conflict with other rules simply because it is so broad.
  • Performance: In modern browsers, the performance difference is almost zero and not worth worrying about. However, the theory is that child selectors (>) are *fractionally* faster because the browser only has to check one level down the DOM, whereas a descendant selector (space) requires the browser to check *all* levels. **Rule of thumb: Choose selectors for clarity and maintainability, not micro-performance.**
Key Takeaway: Use the most specific combinator that achieves your goal. Start with the descendant selector (space) for general context, but immediately reach for the child combinator (>) or sibling combinators (+, ).

Glossary of Terms

Descendant Selector (space)
Example: div p. Selects any p element that is *anywhere* inside a div, at any level of nesting.
Child Combinator (>)
Example: div > p. Selects only p elements that are **direct children** (one level down) of a div.
Adjacent Sibling Combinator (+)
Example: h1 + p. Selects the one p element that comes **immediately after** an h1. They must share the same parent.
General Sibling Combinator (~)
Example: h1 ~ p. Selects **all** p elements that come *after* an h1, as long as they share the same parent.
Ancestor
An element that contains another element. A parent, grandparent, great-grandparent, etc. The descendant selector (space) targets elements within any ancestor.
Parent
The element that *immediately* contains another element. The child combinator (>) is based on this relationship.
Sibling
Elements that share the same parent. Sibling combinators (+, ~) operate on these.
Specificity
The algorithm used by browsers to determine which CSS rule is applied if multiple rules target the same element. Combinators themselves do not add to specificity, but the selectors they connect do.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS Selectors Level 4 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!