CSS: combinator selectors
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
- Combinators can be chained. e.g.
div > ul li + p - The selector expressions in Combinators can be compound selectors. e.g.
div.x > h2 + p.y.
Combinator Operators Precedence
from loose to tight:
(space) descendant combinator>Child combinator+Adjacent sibling~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
- CSS: type selector (tag name)
- CSS: universal selector (* any tag)
- CSS: class selector (.x)
- CSS: ID selector (#)
- CSS: attribute selector ([x])
Combinators
- CSS: descendant selector (space)
- CSS: child selector (>)
- CSS: adjacent sibling selector (+)
- CSS: subsequent sibling selector (~)
Selector list
Nesting syntax
Special selector
- CSS: :root selector
- CSS: no child selector
- CSS: first child, last child, only child selectors
- CSS: nth-child selector
- CSS: nth-child arg of selector
- CSS: nth-of-type selector
- CSS: pseudo-class selector (:)
- CSS: pseudo-element selector (::)
- CSS: :not negation selector
- CSS: :has descendant selector