CSS :nth-last-child() Pseudo-Class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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

Pattern Values

Pass one pattern; indices are counted from the last sibling:

PatternMatches (from end)Try it
1 / :last-childOnly the last childTry it
-n+3Last three childrenTry it
odd / 2n+11st, 3rd, 5th… from the endTry it
2Second-to-last onlyTry it
even / n+2Even from end / all but the lastAn+B

How to Use :nth-last-child()

Count from the end of the sibling list, then style matching positions.

  1. Put items under one parent (ul, tbody, …).
  2. Decide “from the end”: last one, last three, odd from the end, …
  3. Write li:nth-last-child(pattern).
  4. Remember: every sibling tag still occupies a slot in the count.
PieceRequired?Role
Shared parentYesSiblings share one child list
PatternYesodd, even, number, or An+B from the end
Element typeUsuallyMust also match (e.g. li) at that reverse index
of filterOptionalCount only among a filtered subset from the end

Live Preview

Change the pattern. Highlighted items match li:nth-last-child(…) (count from the bottom).

Rule shown: li:nth-last-child(-n+3)

List
  • 1 (start)
  • 2
  • 3
  • 4
  • 5
  • 6 (end)

Matching the last three children

CSS Examples

Short reverse-count patterns. Use Try It Yourself for a live editor.

Example 1 — Last Three (-n+3)

Highlight the last three siblings.

CSS
<style>
  li:nth-last-child(-n+3) {
    background: #dbeafe;
  }
</style>

<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>
Try It Yourself

How It Works

  1. Count from the end. Five=1, Four=2, Three=3.
  2. -n+3 for n = 0,1,2 yields 3, 2, 1 — the last three.
  3. One and Two are farther from the end, so they stay plain.
  4. Useful for “show last N” without extra classes.

Example 2 — Second-to-Last

Style only the sibling just before the last.

CSS
<style>
  li:nth-last-child(2) {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
</ul>
Try It Yourself

How It Works

  1. From the end: D=1, C=2, B=3, A=4.
  2. 2 matches C — second-to-last.
  3. 1 would match D — same as :last-child.
  4. Stable when items are appended — “penultimate” stays correct.

Example 3 — Odd from the End

Zebra-style stripes counted from the last item.

CSS
<style>
  li:nth-last-child(odd) {
    background: #fef3c7;
  }
</style>

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
</ul>
Try It Yourself

How It Works

  1. From the end: 4=odd, 3=even, 2=odd, 1=even.
  2. Items 4 and 2 get the highlight (odd reverse indices).
  3. Not the same as :nth-child(odd) when the total count is even or odd — compare both.
  4. 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.

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 Chrome Supported
Full An+B · of modern
Mozilla Firefox Supported
Full An+B · of modern
Apple Safari Supported
Full An+B · of modern
Microsoft Edge Supported
Full An+B · of modern
Opera Supported
Full An+B
Samsung Internet Supported
Full An+B
Internet Explorer 9+
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.

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.

Next: :nth-last-of-type()

Learn same-element-type counting from the end of the sibling list.

:nth-last-of-type() tutorial →

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