CSS: subsequent sibling selector (~)
Subsequent-Sibling Combinator
tag1 ~ tag2-
match element x if:
- tag2 matches x.
- x has a older sibling y (y comes before x), tag1 matches y.
(think of it as match all tag2 that has a older brother tag1.)
tag can be a simple selector or compound selector.
/* make list items red, except first */ li ~ li { color: red; } <ul> <li>cat</li> <li>dog (match)</li> <li>bird (match)</li> </ul>
Example. All paragraphs after a heading
/* all p elements that are siblings of h2 and come after it */ h2 ~ p { font-weight: bold; }
CSS selectors by child or sibling rank
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