CKTIS / CSS
:nth-last-child() — counting elements from the end
The :nth-last-child() pseudo-class selects elements by their position counted backwards among siblings. Optional of S filters elements before counting.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:nth-last-child(An+B [of S]):nth-last-child() counts elements backwards, starting at 1; text and comments are ignored. Without of S, all sibling elements count, regardless of type.
An+B generates positions for integers n ≥ 0; only positive results count. 2 means second from last, -n+2 means the last two, odd equals 2n+1, and even equals 2n.
Optional of S filters before counting. .item:nth-last-child(2) and :nth-last-child(2 of .item) are not equivalent. Specificity is (0,1,0) plus the highest selector specificity in S.
:nth-child() counts forwards; :nth-last-of-type() counts backwards only among elements of the same type.
Examples
<!doctype html>
<meta charset="utf-8">
<title>:nth-last-child()</title>
<style>
#items > :nth-last-child(2 of .item) {
background-color: yellow;
}
</style>
<ul id="items">
<li class="item">A</li>
<li class="item">B</li>
<li>C</li>
<li class="item">D</li>
<li>E</li>
</ul>Result
B: background-color = rgb(255, 255, 0)
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.