CSS Combinators
-
Learn CSS combinators to target elements based on their relationship with other elements.
Defining Relationships Between Elements
What Are CSS Combinators ?
CSS combinators are used to define relationships between HTML elements.
They tell CSS how elements are connected to each other:
• Parent → child
• Ancestor → descendant
• One element → its siblingsIn simple words:
Combinators help CSS understand “who is related to whom.”
Instead of styling everything blindly, combinators let you say:
Style elements inside something
Style elements directly under something
Style elements next to somethingWhy Are CSS Combinators Important ?
Without combinators:
Too many elements get styled
Unwanted side effects
CSS becomes hard to manageWith combinators:
Precise and targeted styling
Real-life comparison:
Cleaner, readable CSS
Better performance
“Only students sitting in the first row should raise their hands.”
That’s exactly how combinators work.
Types of CSS Combinators
CSS provides four main combinators:
Descendant combinator (space)
Child combinator (>)
Adjacent sibling combinator (+)
General sibling combinator (~)
Descendant Combinator (space)
What it does:
Selects elements that are inside another element, at any depth.
Syntax:
parent descendant {
property: value;
}
CSS Descendant Combinator (Space Selector)
The descendant combinator selects all elements inside a parent element, no matter how deeply nested.
<style>
div p {
color: blue;
}
</style>
<div>
<p>This is styled</p>
<section>
<p>This is also styled</p>
</section>
</div>
Real-life comparison:
“All employees inside the office building must wear ID cards.”Child Combinator (>)
What it does:
Selects only direct children of an element.
Syntax:
parent > child {
property: value;
}
Child Combinator ( > )
The child combinator selects only direct child elements of a parent, not deeply nested elements.
<style>
div > p {
color: red;
}
</style>
<div>
<p>Styled</p>
<section>
<p>Not styled</p>
</section>
</div>
Real-life comparison:
“Only children of this house - not guests.”Descendant vs Child
Adjacent Sibling Combinator (+)Descendant Child All levels Only direct Uses space Uses > Broad Strict What it does:
Selects the immediately next sibling.
Syntax:
element + sibling {
property: value;
}
Adjacent Sibling Combinator ( + )
The adjacent sibling combinator selects the element that comes immediately after another element.
<style>
h1 + p {
color: green;
}
</style>
<h1>Heading</h1>
<p>This is styled</p>
<p>This is NOT styled</p>
Real-life comparison:
“The person standing immediately next to you.”General Sibling Combinator (~)
What it does:
Selects all siblings that come after a specified element.
Syntax:
element ~ sibling {
property: value;
}
General Sibling Combinator ( ~ )
The general sibling combinator selects all sibling elements that come after a specified element.
<style>
h1 ~ p {
color: purple;
}
</style>
<h1>Heading</h1>
<p>Styled</p>
<p>Styled</p>
- Real-life comparison:
“All students sitting after the first bench.”
Combinators Complete Practical
This example demonstrates how different CSS combinators (descendant, child, adjacent sibling, and general sibling) select elements in various relationships.
<style>
.container p {
color: blue; /* Descendant */
}
.container > p {
font-weight: bold; /* Child */
}
h2 + p {
color: red; /* Adjacent sibling */
}
h2 ~ p {
background-color: #f2f2f2; /* General sibling */
}
</style>
<div class="container">
<h2>Title</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
<section>
<p>Nested paragraph</p>
</section>
</div>
All <p> inside .container → blue
Direct child <p> → bold
First <p> after <h2> → red
All <p> after <h2> → gray backgroundCommon Mistakes
Confusing descendant with child
Forgetting the space in selectors
Using very broad selectors
Not understanding sibling orderBest Practices for CSS Combinators :
Use child combinator for strict control
Avoid deeply nested descendant selectors
Keep selectors readable and simple
Use sibling combinators for contextual styling• Combinators define relationships
• Descendant → inside
• Child → direct only
• Adjacent sibling → immediate next
• General sibling → all afterCSS combinators control element relationships
Help write precise and clean CSS
Improve performance and maintainability
Essential for advanced styling
Very important for interviews & real projects