In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based on how they are positioned within the HTML structure.
Software developers are building the future. Become part of them.
There are four main CSS combinators:
space)>)+)~)Let’s break each down—definitions, diagrams, and examples.
– space)Selects any element nested inside another, at any depth.
A B = Select B only if it exists anywhere inside A.
<div class="container">
<p>First paragraph</p>
<div>
<p>Nested paragraph</p>
</div>
</div> .container p {
color: blue;
} All <p> elements inside .container—no matter how deeply nested—turn blue.
>)Selects direct children only, not deeper descendants.
A > B = Select B only if it is a direct child of A.
<ul class="menu">
<li>Home</li>
<li>
About
<ul>
<li>Team</li>
</ul>
</li>
</ul> .menu > li {
font-weight: bold;
} Only the top-level list items (Home and About) become bold, not the nested “Team” item.
+)Selects the immediately next sibling element.
A + B = Select B only if it comes right after A.
<h2>Title</h2>
<p>Subtitle paragraph</p>
<p>Another paragraph</p> h2 + p {
margin-top: 0;
} Only the first <p> after <h2> is affected.
~)Selects all siblings that come after a specified element—not just the first one.
A ~ B = Select every B that appears after A in the same parent.
<div>
<h3>Header</h3>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div> h3 ~ p {
color: green;
} All <p> elements after <h3> turn green.
| Combinator | Symbol | Selects | Example | Meaning |
|---|---|---|---|---|
| Descendant | (space) | Any nested element | div p | All <p> inside <div> |
| Child | > | Direct children | ul > li | Only top-level <li> |
| Adjacent Sibling | + | Next element only | h1 + p | Only the first <p> after <h1> |
| General Sibling | ~ | All following siblings | h1 ~ p | All <p> after <h1> |
Use them sparingly in large apps for performance.
They prevent styles from leaking into unintended nested elements.
Example:
input.error + small {
color: red;
} Example:
.card > h2 + p {
opacity: 0.7;
} Meaning: Select the <p> that comes right after <h2> inside .card.
CSS combinators are essential for writing clean, predictable, and efficient styles. They allow you to target elements based on their structural relationship, making your CSS more modular and powerful.
Latest tech news and coding tips.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…
In the world of software development—where new frameworks appear overnight, job titles evolve every three…