The :nth-last-child() pseudo-class targets elements by their position counting from the end of the sibling list. It is the reverse counterpart to :nth-child().
01
Reverse count
1 = last child.
02
odd / even
From the end.
03
2n, 3n+1
Same formulas.
04
-n+3
Last N items.
05
vs nth-child
Know the diff.
06
= :last-child
Position 1.
Fundamentals
Introduction
The :nth-last-child() selector in CSS lets you target elements based on their position within a parent — but counting from the last child backward instead of from the first.
It is especially useful when you need to style the last few items, highlight the final row, or apply reverse-order patterns without rearranging your HTML.
Definition and Usage
Write element:nth-last-child(n) where n is a number, the keyword odd or even, or a formula like 2n+1 or -n+3.
💡
Beginner Tip
Counting from the end also starts at 1. :nth-last-child(1) is the last child — the same as :last-child. :nth-last-child(2) is the second-to-last child.
Foundation
📝 Syntax
The syntax for the :nth-last-child() pseudo-class is:
syntax.css
element:nth-last-child(n){/* CSS properties */}
element is the tag you want to style. n can be a number, keyword, or an+b formula.
Common Patterns
Expression
Matches (5-item list)
:nth-last-child(1)
Item 5 (last child)
:nth-last-child(2)
Item 4 (second-to-last)
:nth-last-child(odd)
Items 5, 3, 1 (odd from end)
:nth-last-child(even)
Items 4, 2 (even from end)
:nth-last-child(2n)
Items 4, 2 (every 2nd from end)
:nth-last-child(-n+3)
Items 5, 4, 3 (last three)
The an+b Formula (Reverse Direction)
n is a counter starting at 0, increasing by 1 for each child from the end.
a is the step size — positive steps skip toward the start; negative steps select the last N items.
b is the offset — the starting position when counting from the last child.
Odd from the end means positions 1, 3, 5 from the last child — which are items 5, 3, and 1 in a five-item list. This differs from :nth-child(odd), which highlights items 1, 3, 5 from the start.
📄 Targeted Positions
Pinpoint specific reverse positions and select the last N siblings.
Example 3 — Highlight the second-to-last item
Use :nth-last-child(2) to style the item just before the final one.
Position 2 from the end is Item 4 in a five-item list. This is handy for styling a penultimate row or a “load more” button placed before the last item.
Example 4 — Style the last three items
The -n+3 formula selects the last three children in any list length.
When n=0, the formula gives 3. When n=1, it gives 2. When n=2, it gives 1. The negative step stops after three matches — a powerful pattern for “recent items” styling.
Tips
💬 Usage Tips
Prefer :last-child for one item — :nth-last-child(1) works, but :last-child is more readable.
Use -n+k for last N — -n+3 last three, -n+5 last five.
Combine selectors — ul li:nth-last-child(2) scopes to list items inside a ul.
Test formulas — Plug n=0,1,2 into an+b to verify which reverse positions match.
When type matters — Switch to :nth-last-of-type() if mixed tags break your count.
Watch Out
⚠️ Common Pitfalls
Counts from the end, not the start — :nth-last-child(odd) is not the same as :nth-child(odd).
Counts all children — A <div> before your <li> shifts the count; use :nth-last-of-type() if needed.
1-based, not 0-based — The last child is :nth-last-child(1), not 0.
Empty text nodes — Whitespace-only text nodes between elements can affect counting in some parsers.
Overlapping rules — Multiple :nth-last-child() rules can apply to the same element; watch specificity.
A11y
♿ Accessibility
Contrast on highlights — Last-item or last-N styling must still meet WCAG contrast requirements.
Do not rely on color alone — Pair visual cues with text labels or icons where meaning matters.
Semantic lists — Use proper <ul>/<ol> markup for screen readers.
Dynamic content — When items are added via JavaScript, reverse selectors stay anchored to the end without extra classes.
🧠 How :nth-last-child() Works
1
Browser lists siblings
All children inside the parent are numbered from the last child backward, starting at 1.
Reverse index
2
Evaluates n expression
The value inside :nth-last-child() is tested against each reverse position.
Match
3
Filters by element
Only elements matching the tag before :nth-last-child() receive styles.
Filter
=
📊
End-anchored styling
Last items, reverse stripes, and last-N patterns appear without extra HTML classes.
Compatibility
🖥 Browser Compatibility
:nth-last-child() is supported in all modern browsers and has been available since CSS3 Selectors.
✓ Baseline · Universal support
Reverse positional selectors everywhere
:nth-last-child() works reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:nth-last-child() pseudo-class99% supported
Bottom line::nth-last-child() is safe for lists, tables, and end-anchored layout patterns in all modern projects.
Wrap Up
🎉 Conclusion
The CSS :nth-last-child() selector offers a powerful way to style elements based on their position from the end within a parent container. It enables reverse-order patterns without rearranging HTML.
From highlighting the last item to selecting the last three siblings with -n+3, mastering :nth-last-child() pairs naturally with :nth-child() for complete positional control.
Scope with parent selectors like ul li:nth-last-child()
Use :last-child when you only need the final item
Combine with :not() for refined patterns
❌ Don’t
Confuse reverse odd with forward odd
Assume counting starts at 0
Forget that all sibling types are counted
Use nth-last-child(1) when :last-child is clearer
Rely on highlight color alone for meaning
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :nth-last-child()
Use these points when styling from the end.
5
Core concepts
←n01
Reverse count
1 = last child.
Purpose
odd02
Keywords
odd / even.
Syntax
-n+303
Last N items
Negative step.
Pattern
≠04
vs nth-child
End vs start.
Compare
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :nth-last-child() pseudo-class matches elements based on their position among sibling children, counting from the end of the list instead of the beginning. You pass a number, odd, even, or an an+b formula inside the parentheses.
Yes. :nth-last-child(1) selects the last child in a parent, which is identical to :last-child. Use whichever reads more clearly in your stylesheet.
:nth-child() counts from the first sibling (1, 2, 3…). :nth-last-child() counts from the last sibling (1 = last, 2 = second-to-last, 3 = third-to-last, and so on). The formulas work the same way, but the direction is reversed.
Use :nth-last-child(-n+3). The negative step selects from the end: it matches the last child, second-to-last, and third-to-last.
Yes. All modern browsers support :nth-last-child(). It has been available since CSS3 Selectors and works reliably for lists, tables, and grids.