Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

What Is the Combinator in CSS: Combinator in CSS

Understand What Is the Combinator in CSS: Combinator in CSS with clear explanations, practical examples, and useful tips. This updated guide covers the...

Table of Contents

What Is the Combinator in CSS: Combinator in CSS is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Combinator (combination) shows the relationship between selectors, allowing combining selectors together as characters.

In CSS, there are 4 familiar combinators as follows:

  • Descendant combinator (space): Select elements that are descendants of another element.
  • Child combinator (>): Select elements that are children of another element.

Note here that "descendants" help denote an element in another element, while "child" is the element directly located inside of another element. Eg

example

only span is the "child" of the div, and both span and p are "descendants" of the div.

  • Adjacent sibling combinator (+): Select elements located adjacent and on par with other elements ("brother" elements are adjacent).
  • General sibling combinator (~): Select element equal to another element ("brother" elements).

Descendant combinator

Descendant combinator (space) allows combining elements as descendants of another element.

 div p { 
 background-color: yellow; 
 } 

Here, descendants are p tags, the first element is div. The p tag in the div tag will have background-color: yellow.

As an example,:

What Is the Combinator in CSS: Combinator in CSS example image 1

Example Descendant combinator

Paragraph 1 is in the div.

Paragraph 2 is in the div.

Paragraph 3 is in the div.

Paragraph 4 is not in the div.

Paragraph 5 is not in the div.

Child combinator

Child combinator (>) allows combining "direct" sub-elements of another element (as explained above).

 div > p { 
  background-color: yellow; 
 } 

Here, the child element is p tag , the first element is div. The p tag in the div tag will have a background-color: yellow, and the other tags in p will not.

As an example,:

What Is the Combinator in CSS: Combinator in CSS example image 2

Example Descendant combinator

Paragraph 1 is in the div.

Paragraph 2 is in the div.

Paragraph 3 is in the div.

Paragraph 4 is not in the div.

Paragraph 5 is not in the div.

Adjacent sibling combinator

Adjacent sibling combinator (+) allows to select elements that are adjacent and on par with the specified element (adjacent "brother" elements).

 div + p { 
  background-color: yellow; 
 } 

Here, the element p is adjacent to and equal to the div, there will be background-color: yellow , and the other p tags in the div or p- cards are equal but not adjacent to the div .

As an example,:

What Is the Combinator in CSS: Combinator in CSS example image 3

Example Adjacent sibling combinator

Paragraph 1 is in the div.

Paragraph 2 is in the div.

Paragraph 3 is in the div.

Paragraph 4 is not in the div.

Paragraph 5 is not in the div.

General sibling combinator

General sibling combinator (~) allows to select elements that are equal to the specified element ("brother" elements).

 div ~ p { 
  background-color: yellow; 
 } 

Here, the element p is on the same level as the div will have background-color: yellow , and the other p tags in the div will not.

As an example,:

What Is the Combinator in CSS: Combinator in CSS example image 4

Example General sibling combinator

Paragraph 1 is in the div.

Paragraph 2 is in the div.

Paragraph 3 is in the div.

Paragraph 4 is not in the div.

Paragraph 5 is not in the div.

Previous post: Alignment - Align in CSS

Next lesson: Pseudo-class in CSS

FAQ

What should you know about descendant combinator?

Descendant combinator (space) allows combining elements as descendants of another element.

What should you know about child combinator?

Child combinator (>) allows combining "direct" sub-elements of another element (as explained above).

What should you know about adjacent sibling combinator?

Adjacent sibling combinator (+) allows to select elements that are adjacent and on par with the specified element (adjacent "brother" elements).

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.