CSS Combinators

CSS combinators are used to define the relationship between two selectors. Understanding CSS combinators helps in creating more precise and effective CSS rules. There are four types of combinators in CSS: descendant combinator, child combinator, adjacent sibling combinator, and general sibling combinator.

Descendant Combinator

The descendant combinator is represented by a space between two selectors. It targets elements that are descendants of a specified element.

For example, the following CSS rule selects all <p> elements that are descendants of a <div> element:

Try yourself
        
            div p {
    color: blue;
}
        
    

In this example, all <p> elements within any <div> will be styled according to the CSS rule.


Child Combinator

The child combinator is represented by a greater-than sign (>) between two selectors. It targets elements that are direct children of a specified element.

For example, the following CSS rule selects all <p> elements that are direct children of a <div> element:

Try yourself
        
            div > p {
    color: green;
}
        
    

In this example, only the <p> elements that are direct children of a <div> will be styled according to the CSS rule.


Adjacent Sibling Combinator

The adjacent sibling combinator is represented by a plus sign (+) between two selectors. It targets an element that is immediately preceded by a specified element.

For example, the following CSS rule selects the <p> element that immediately follows a <h1> element:

Try yourself
        
            h1 + p {
    color: red;
}
        
    

In this example, the first <p> element immediately following a <h1> will be styled according to the CSS rule.


General Sibling Combinator

The general sibling combinator is represented by a tilde (~) between two selectors. It targets all elements that are siblings of a specified element.

For example, the following CSS rule selects all <p> elements that are siblings of a <h1> element:

Try yourself
        
            h1 ~ p {
    color: purple;
}
        
    

In this example, all <p> elements that are siblings of a <h1> will be styled according to the CSS rule.


Comparing CSS Combinators

Understanding the differences between these combinators helps you create more efficient and specific CSS rules. Here are comparisons of the different combinators:

Descendant combinator is less specific and targets all matching descendants, while child combinator targets only direct children, making it more specific. Adjacent sibling combinator targets only the immediately following sibling, while general sibling combinator targets all siblings.

Important Notes

Here are some important notes and best practices when using CSS combinators:

Use descendant combinators sparingly as they can be less performant, especially in large documents. Use child combinators when you need more specificity and control over the parent-child relationships. Use adjacent sibling combinators to style elements immediately following a specified element, and general sibling combinators when you need to target all siblings.