CSS: nth-of-type selector
First-of-type, last-of-type
tag:first-of-type-
match element x if:
- tag matches x.
- of all x's siblings, consider only those matched by tag, x is the first.
tag is optional. It can be a simple selector (selecting by tag name, id, class etc) or a compound selector (combination of tag name, id, class etc). If empty, it means the universal selector (* any tag).
p:first-of-type { color: red; } <div> <div> Zs3vx </div> <p>aa ✅</p> <p>bb</p> <div> <p class="xx">cc ✅</p> <p class="xx">dd</p> </div> <p>ee. no match here because p is not first child.</p> </div> tag:last-of-type-
similar to
:first-of-typebut match last.
Nth-of-type, nth-last-of-type
tag:nth-of-type(arg)-
equivalent to
tag:nth-last-of-type(arg)-
similar to
:nth-of-typebut counting starting from end.
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