Home 〉 CSS Tutorials 〉 CSS Combinators Explained with Examples | Child, Descendant, Sibling Selectors
CSS Combinators Explained with Examples | Child, Descendant, Sibling Selectors !
Confused about selecting specific elements in CSS? Combinators like child (`>`), descendant (space), adjacent sibling (`+`), and general sibling (`~`) help you apply styles to elements based on their relationship in the HTML structure. This is essential when working with deeply nested tags. Understanding combinators helps write cleaner, more targeted styles. Here is the detailed blog post on the same.
In CSS, combinators define the relationship between two selectors. They help you select elements based on their location or relationship in the HTML document. There are four types of combinators:
Let's understand each with real examples.
Selects all elements that are descendants of a specified element.
<!DOCTYPE html>
<html>
<head>
<style>
.d-sel p {
color: green;
}
</style>
</head>
<body>
<div class="d-sel">
<p>This paragraph is inside a div.</p>
</div>
</body>
</html>
output 📌
This paragraph is inside a div of class 'd-sel'.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
✅ In this example, any '<p>' inside a '<div>' will be styled with green color.
Selects only direct children of an element.
<!DOCTYPE html>
<html>
<head>
<style>
.child-sec > li {
color: blue;
}
</style>
</head>
<body>
<ul class="child-sec">
<li>Direct child</li>
<ul class="child-sec">
<li>Nested child</li>
</ul>
</ul>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
✅ Only the top-level '<li>' elements inside '<ul>' whose class is 'child-sec' will be blue.
Selects an element that is immediately next to another element.
<!DOCTYPE html>
<html>
<head>
<style>
h1 + p {
color: red;
}
</style>
</head>
<body>
<h1>Heading</h1>
<p>This paragraph comes right after h1.</p>
<p>This paragraph won't be selected.</p>
</body>
</html>
output 📌
This paragraph comes right after h1.
This paragraph won't be selected.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
✅ Only the first '<p>' right after '<h1>' is selected.
Selects all sibling elements that follow the specified element.
<!DOCTYPE html>
<html>
<head>
<style>
h2 ~ p {
color: purple;
}
</style>
</head>
<body>
<h2>Subheading</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
</body>
</html>
output 📌
First paragraph
Second paragraph
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
✅ All '<p>' elements after '<h2>' are styled.
CSS combinators give you powerful tools to select elements based on relationships. Using them efficiently helps keep your CSS clean and structured.
A descendant selects any nested element, while a child selector targets only direct children.
Yes. Example: '.container > .item' selects direct children with class '.item'.
Yes, all modern browsers support CSS combinators.