Sibling combinator in CSS
Tags: css, webdev, beginners, programming
Published: 2022-02-22 14:53:59

There may be some confusion as what we are calling sibling. So, let's first get that out of the way. See the below code:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sibling combinator</title>
</head>
<body>
    <div class="div1">
        <h2>1st Heading</h2>
    </div>
    <div class="div2">
        <p>Lorem ipsum text</p>
        <h2>2nd Heading</h2>
        <p>Lorem ipsum  text</p>
        <p>Lorem ipsum text</p>
    </div>
</body>
</html>
```
In the above code:<br/>
h2(1st heading) has no sibling (parent: .div1)<br/>
p, h2(2nd heading), p and p are siblings (parent: .div2)<br/>
classes div1 and div2 are siblings (parent: body)<br/>

 There are 2 types of sibling combinators:<br/>
1. Adjacent sibling combinator 
2. General sibling combinator

Let's first understand the Adjacent sibling combinator.<br/> 
Here's what MDN has to say: The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.

Let's code it out to understand better.

Example 1:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sibling combinator</title>
    <style>
        h2 + p{
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="div1">
        <h2>1st Heading</h2>
    </div>
    <div class="div2">
        <p>1st para</p>
        <h2>2nd Heading</h2>
        <p>2nd para/p>
        <p>3rd para</p>
    </div>
</body>
</html>
```
![1.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1645538355654/0nIs2FFxD.PNG)
As you can see above, the 2nd para in div2 turned pink as it was immediately following the 2nd heading and both are siblings.

Example 2:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sibling combinator</title>
    <style>
        p + p{
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="div1">
        <h2>1st Heading</h2>
    </div>
    <div class="div2">
        <p>Lorem ipsum text</p>
        <p>Lorem ipsum text</p>
        <h2>2nd Heading</h2>
    </div>
```
![2.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1645538537828/GmHMTGuwJ.PNG)

As you can see above, the 2nd para in div2 turned pink as it was immediately following the 1st para and both are siblings.


Example 3:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sibling combinator</title>
    <style>
        p + p{
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="div1">
        <h2>1st Heading</h2>
    </div>
    <div class="div2">
        <p>1st para</p>
        <h2>2nd Heading</h2>
        <p>2nd para</p>
    <div>
```
![3.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1645537327431/oesgeTESB.PNG)

Can You guess why the 2nd paragraph does not turn pink?<br/>
Because in div2, the 2nd para is not immediately following the 1st para. There is an "h2" in between. 

Now, let's move on to the general combinator.<br/>
Here's what MDN has to say: The general sibling combinator (~) separates two selectors and matches all iterations of the second element, that are following the first element (though not necessarily immediately), and are children of the same parent element.

Let's get to the best practice of understanding with an example.

Example 4:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sibling combinator</title>
    <style>
        p + p{
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="div1">
        <h2>1st Heading</h2>
    </div>
    <div class="div2">
        <p>1st para</p>
        <h2>Heading</h2>
        <p>2nd para</p>
        <h2>Heading</h2>
        <p>3rd para</p>
        <h2>Heading</h2>
        <p>4th para</p>
```
![4.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1645539606028/k-2IuygPE.PNG)

In the above code, it does not matter if there is "h2" in between. All the para's following the 1st para turned pink.

Although it might be obvious, still know this: In the above examples I used the element name as a selector but you can use any selector (class, id, etc.)

That's all folks.

**If you have any doubt ask me in the comments section and I'll try to answer as soon as possible.**

**I write one article every day related to web development (yes, every single day). Follow me here if you are learning the same..**

**If you love the article follow me on Twitter:** @therajatg

**If you are the Linkedin type, let's connect**: https://www.linkedin.com/in/therajatg/

**Have an awesome day ahead 😀!**