CSS: complex selector (combinator)

By Xah Lee. Date: . Last updated: .

Complex selector

A selector that contains at least one combinator operator is called a complex selector.

Combinators (Relationship Selectors)

Combinator operators define the relationship between selectors.

Descendant combinator

e.g. div p

> Child combinator

e.g. div > p

+ Adjacent sibling

e.g. h2 + p

~ Subsequent sibling

e.g. h2 ~ p

Combinator Operators Precedence

from loose to tight:

  1. (space) descendant combinator
  2. > Child combinator
  3. + Adjacent sibling
  4. ~ Subsequent sibling

you cannot use parenthesis to specify syntax precedence.

For example, this:

.a > .b .c + .d { color: red; }

means:

((.a > .b) (.c + .d))

not the following:

(.a > (.b .c)) + .d

((.a > .b) .c) + .d

CSS. Selectors

Selector types
Simple selectors
Combinators
Selector list
Special selector
Misc