CSS Combinators
CSS Combinators
![]() |
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 (separated by a space)
- Child selector (separated by the greater-than > sign)
- Adjacent sibling selector (separated by the plus + sign)
- General sibling selector (separated by the tilde ~ sign)
Descendant Selector
A descendant selector is used to select descendant elements of a given element.
The following example selects all
element into the
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
:
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
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
:
Example
div
~
p
{
background-color:
yellow
;
}
Try it »
