CSS Combinators
In this page:
ancestor descendant { }
parent > child { }
element + adjacent-sibling { }
element ~ general-sibling { }
The Descendant Selector (Space)
The descendant selector targets every matching element nested anywhere inside a specified parent container, regardless of how deeply they are nested (children, grandchildren, great-grandchildren, etc.). It is defined by a simple space between selectors.
Example: The Descendant Selector (Space)
<style>
.card p {
color: gray;
}
</style>
<div class="card">
<p>Nested paragraph</p>
<div><p>Deeply nested paragraph</p></div>
</div>
The Child Selector (>)
The child selector (defined by the > symbol) targets only direct children of a specified parent container, ignoring any elements nested more deeply (like grandchildren or great-grandchildren).
Example: The Child Selector (>)
<style>
.menu > li {
color: blue;
}
</style>
<ul class="menu">
<li>Top-level item</li>
<li><ul><li>Nested item</li></ul></li>
</ul>
The Adjacent Sibling Selector (+)
The adjacent sibling selector (defined by the + symbol) targets the very next sibling element located directly after a specified element on the same layout level.
Example: The Adjacent Sibling Selector (+)
<style>
h2 + p {
font-weight: bold;
}
</style>
<h2>Heading</h2>
<p>Immediately after heading</p>
<p>Not affected</p>
The General Sibling Selector ()
The general sibling selector (defined by the ~ symbol) targets all matching sibling elements situated anywhere after a specified element on the same layout level, regardless of intervening elements.
Example: The General Sibling Selector ()
<style>
h2 ~ p {
color: green;
}
</style>
<h2>Heading</h2>
<p>Affected sibling</p>
<p>Also affected sibling</p>
Combining Multiple Selectors
You can combine multiple different combinators in a single selector rule to build complex target paths that target elements with extreme precision.
Example: Combining Multiple Selectors
<style>
.card > h2 + p {
color: purple;
}
</style>
<div class="card">
<h2>Title</h2>
<p>Styled paragraph</p>
</div>
- Using descendant selectors (space) when you only want to target direct children, causing accidental styling leaks.
- Forgetting that adjacent sibling selectors (+) only match the single element located immediately next to the trigger.
- Designing excessively long, fragile compound selector chains that break easily when you update your HTML structure.
- Combinators select target elements based on their relationships in the HTML family tree structure.
- The space selector targets all descendants, while the > selector targets only direct children of a specified parent.
- The + selector targets the single adjacent sibling located immediately next to the trigger, while the ~ selector targets all sibling elements situated anywhere after it.
Standard CSS combinators (+, >, ) are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline