Sibling combinator in css

Sibling combinator in css

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:

<!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:

  1. h2(1st heading) has no sibling (parent: .div1)
  2. p, h2(2nd heading), p and p are siblings (parent: .div2)
  3. classes div1 and div2 are siblings (parent: body)


There are 2 types of sibling combinators:

  1. Adjacent sibling combinator
  2. General sibling combinator

Let's first understand the Adjacent sibling combinator.

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:

<!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>        

[Click to continue reading...]

To view or add a comment, sign in

More articles by Rajat Gupta

  • pseudo classes in css part 1 (:hover)

    Note: This is the first part of the series dedicated to the pseudo-classes of CSS. In this part, we'll understand the…

  • useContext in react: everything you need to know

    only understand this: Using useContext is not necessary, we use it to avoid prop drilling. Here's how prop drilling…

  • rest parameter in javascript

    The rest parameter is introduced in ES6. It allows a function to accept an indefinite number of arguments as an array…

    1 Comment
  • PropTypes in react

    Let's see what reactjs.org has to say: As your app grows, you can catch a lot of bugs with type-checking.

  • What the heck are props in react

    Although we can make web apps using JavaScript. One of the reasons we are using react over JS is component reusability.

  • JavaScript: single-threaded and synchronous

    A few days ago single threaded and synchronous were just 2 heavy words for me. If this is you right now, don't worry…

    1 Comment
  • reduce() method in JavaScript

    Let's see what MDN has to say: The reduce() method executes a user-supplied “reducer” callback function on each element…

  • filter() method in JavaScript

    Definition by MDN: The filter() method creates a new array with all elements that pass the test implemented by the…

  • map() method in JavaScript

    Let's see what MDN has to say: The map() method creates a new array populated with the results of calling a provided…

  • object-fit property in CSS

    The object-fit CSS property sets how the content which includes images, videos and other embedded media formats should…

Insights from the community

Others also viewed

Explore topics