visit
Hey š, I am Kunaal. In the article, we'll talk about something called CSS Combinators. You may have seen something likeĀ
div ~ span
Ā orĀ div > p
Ā and that's exactly what we are going to discuss in this blog. So without ado, let's get started.div
Ā andĀ p
Ā are simple selectors but togetherĀ div p
Ā they are combinator selectors.Basically, the combinator selector defines a relationship between the selectors.div p {
color : red;
}
It will colour/style all theĀ
<p>
Ā elements insideĀ <div>
Ā elements including all the nestedĀ <p>
Ā elements. Nested elements are those that are nested in a container like<div>
<p>Paragraph</p>
<h1><p>Nested Paragraph</p><h1>
</div>
div > p {
color: red;
}
So the above example will select all theĀ
<p>
Ā elements ofĀ <div>
Ā element but it will not affect the nestedĀ <p>
Ā elements.div + p {
color: red;
}
So the above code will only select theĀ
<p>
Ā element which is just after theĀ <div>
Ā element. Example -div ~ p {
color: red;
}
The above example will select all theĀ
<p>
Ā elements after theĀ <div>
Ā element. Example: