CSS Combinators


A combinator is a way to describe how selectors are related to each other.

In HTML, you can use a CSS selector to choose multiple elements. You can also add something called a "combinator" between these selections.

  • descendant selector (space)
  • child selector (>)
  • adjacent sibling selector (+)
  • general sibling selector (~)

Descendant Selector

The descendant selector is used to select all elements that are inside another specified element..

The next example picks out all the <p> paragraphs within <div> containers:;

Example

div p {
  background-color: yellow;
}
Try it Yourself »

Child Selector (>)

The child selector picks out all elements that are directly inside a particular element.

In this example, we're picking all <p> paragraphs inside a <div> box:

Example

div > p {
  background-color: yellow;
}
Try it Yourself »

Adjacent Sibling Selector (+)

The adjacent sibling selector picks an element right next to another particular element.

Siblings are elements that share the same parent element, and 'adjacent' simply means 'right next to.

In this example, we're choosing the very first <p> element that comes directly after <div> elements, like this:

Example

div + p {
  background-color: yellow;
}
Try it Yourself »

General Sibling Selector (~)

The general sibling selector picks all elements that come right after a particular element.

In this example, we target all <p> elements that come right after <div> elements.

Example

div ~ p {
  background-color: yellow;
}
Try it Yourself »