Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Combinators

CSS Combinators


CSS Combinators

Note A combinator explains the relationship between two selectors.

CSS combinators include various ways of combining simple selectors.

CSS3 includes four types of combinators:


Descendant Selector

A descendant selector is used to select descendant elements of a given element.

The following example selects all

element into the

element: 

Example

div

p

{

background-color:

yellow

;

}

Try it »


Child Selector

Compared with descendant selectors, child selectors can only select elements that are direct/first-level children of an element.

The following example selects all

direct child elements within the element

:

Example

div

p

{

background-color:

yellow

;

}

Try it »


Adjacent Sibling Selector

The adjacent sibling selector can select an element that immediately follows another element, and both share the same parent.

If you need to select an element that immediately follows another element and both share the same parent, you can use the adjacent sibling selector.

The following example selects all

the first element after the

element:

Example

div

p

{

background-color:

yellow

;

}

Try it »


General Sibling Selector

The general sibling selector selects all sibling elements that follow a specified element.

The following example selects all

sibling elements after the element

: 

Example

div

~

p

{

background-color:

yellow

;

}

Try it »