The :nth-last-child() pseudo-class matches elements by their 1-based index among siblings, counting from the end. Position 1 is the last child; 2 is second-to-last.
Same patterns as :nth-child() (odd, even, An+B) — only the direction flips. Example: li:nth-last-child(-n+3) styles the last three items.
Baseline — widely available.
Core patterns are long-supported. The newer of <selector> form has narrower historical support — treat it as progressive when you need filtered counting from the end.
Syntax
CSS Syntax
syntax.css
:nth-last-child(<An+B> | even | odd)
Optional filter: :nth-last-child(even of .noted). Examples: :nth-last-child(1), :nth-last-child(2n), :nth-last-child(-n+3).
Patterns
Pattern Values
Pass one pattern; indices are counted from the last sibling:
Items 4 and 2 get the highlight (odd reverse indices).
Not the same as:nth-child(odd) when the total count is even or odd — compare both.
Same as 2n+1 counted from the end.
Edge Cases & Pitfalls
Direction is the main difference from :nth-child() — formulas look the same.
direction
Same formula, opposite end
-n+3 means “first three” with :nth-child() and “last three” with :nth-last-child().
counting
Every sibling still counts
Other tags occupy reverse indices too. Prefer :nth-last-of-type() for same-tag counting from the end.
of
of filters the reverse list
:nth-last-child(2 of .noted) is the 2nd-from-last .noted — not “second-to-last child if it has .noted.” Test support when you need it.
quantity
“At least N items” trick
li:nth-last-child(n+3) (and related sibling patterns) can style lists only when they contain enough items — a common quantity-query pattern.
Compatibility
Browser Support
:nth-last-child() is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.
✓ Baseline · Widely available
:nth-last-child()
odd, even, and An+B from the end have been available across browsers for years (CSS3 Selectors). The of <selector> syntax is newer. Internet Explorer 9+ supports the classic form.
98%Widely available
Google ChromeSupported
Full An+B · of modern
Mozilla FirefoxSupported
Full An+B · of modern
Apple SafariSupported
Full An+B · of modern
Microsoft EdgeSupported
Full An+B · of modern
OperaSupported
Full An+B
Samsung InternetSupported
Full An+B
Internet Explorer9+
Classic An+B · no of
:nth-last-child()Baseline · Widely available
Bottom line: Safe for last-N and reverse odd/even patterns. Use of selectively. This is not a JavaScript runtime API.
Remember
Key Takeaways
Role: select by sibling position counting from the end (1 = last).
Last N::nth-last-child(-n+3) → last three.
Shortcut::nth-last-child(1) = :last-child.
Same-tag from end? Use :nth-last-of-type().
One line::nth-last-child() is :nth-child() turned around — count from the end, same formulas.
Frequently Asked Questions
It matches elements by their 1-based position among siblings, counting from the end. Position 1 is the last child; 2 is second-to-last.
Yes. Both select the last sibling. Prefer :last-child when you only need that one case.
:nth-child() counts from the start. :nth-last-child() uses the same odd/even/An+B patterns but from the end. Formulas look identical; the direction flips.
Use :nth-last-child(-n+3). It matches the last, second-to-last, and third-to-last siblings.
You can write :nth-last-child(-n+3 of .noted) to count only among filtered matches from the end. Support for of is newer than basic An+B.
No. It is a browser CSS selector. Node.js, Deno, and Bun do not apply it.