CSS :nth-last-child() Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Positional

What You’ll Learn

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.

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.

📝 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

ExpressionMatches (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.
li:nth-last-child(1) li:nth-last-child(2n) li:nth-last-child(2) li:nth-last-child(-n+3)

Related Selectors

  • :nth-child() — counts from the first sibling forward
  • :last-child — shorthand for :nth-last-child(1)
  • :nth-last-of-type() — counts only siblings of the same tag type, from the end
  • :not() — combine for exclusions like li:not(:nth-last-child(1))

⚡ Quick Reference

QuestionAnswer
Selector typeFunctional structural pseudo-class
Syntaxelement:nth-last-child(n)
Index starts at1 from the last child (not 0)
CountsAll sibling children (any tag type)
Common useLast item, last N items, reverse zebra stripes
Browser supportAll modern browsers

When to Use :nth-last-child()

:nth-last-child() shines when the end of a list matters more than the start:

  • Highlight the last itemli:nth-last-child(1) or combine with :last-child.
  • Style the last N itemsli:nth-last-child(-n+3) for the final three cards.
  • Reverse zebra stripesli:nth-last-child(2n) stripes from the bottom up.
  • Second-to-last rowtr:nth-last-child(2) for a footer row above the last.
  • Dynamic lists — When items are added at the end, reverse counting stays anchored to the bottom.

👀 Live Preview

The last item is bold red; every second item from the end gets a blue background via :nth-last-child():

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

Examples Gallery

Practice :nth-last-child() with last-item styling, reverse stripes, second-to-last targeting, and the last-three pattern.

📜 Core Patterns

Style elements by reverse position and repeating patterns from the end.

Example 1 — Style the last item and reverse stripes

Make the last list item red with li:nth-last-child(1) and stripe every second item from the end with li:nth-last-child(2n).

nth-last-child-last.css
li:nth-last-child(1) {
  color: #b91c1c;
  font-weight: 700;
}

li:nth-last-child(2n) {
  background: #dbeafe;
}
Try It Yourself

How It Works

2n from the end matches items 4 and 2. The separate :nth-last-child(1) rule styles Item 5 regardless of the stripe pattern.

Example 2 — Odd items counting from the end

Use odd to highlight every odd-positioned child when counting backward.

nth-last-child-odd.css
li:nth-last-child(odd) {
  background: #f1f5f9;
}

/* Equivalent to :nth-last-child(2n+1) */
Try It Yourself

How It Works

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.

nth-last-child-second.css
li:nth-last-child(2) {
  background: #dcfce7;
  border-left: 4px solid #16a34a;
  padding-left: 0.65rem;
}
Try It Yourself

How It Works

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.

nth-last-child-last-three.css
li:nth-last-child(-n+3) {
  background: #fef3c7;
  border-left: 4px solid #d97706;
  padding-left: 0.65rem;
}
Try It Yourself

How It Works

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.

💬 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 selectorsul 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.

⚠️ 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.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:nth-last-child() pseudo-class 99% supported

Bottom line: :nth-last-child() is safe for lists, tables, and end-anchored layout patterns in all modern projects.

🎉 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.

💡 Best Practices

✅ Do

  • Use -n+k for “last N items” patterns
  • Test an+b with n=0,1,2 before deploying
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-last-child()

Use these points when styling from the end.

5
Core concepts
odd 02

Keywords

odd / even.

Syntax
-n+3 03

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.

Practice in the Live Editor

Open the HTML editor and experiment with :nth-last-child(), last-N patterns, and reverse stripes.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful