CSS :nth-last-of-type() Pseudo-Class

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

Definition and Usage

The :nth-last-of-type() pseudo-class matches elements by their position among siblings of the same tag name, counting from the end. Other element types are skipped in that count.

Example: span:nth-last-of-type(2) styles the second-to-last span, even if an em or div sits between spans. Contrast with :nth-last-child(), which counts every sibling.

Baseline — widely available.

Supported across modern browsers since mid-2015 (CSS3 Selectors). Same odd / even / An+B patterns as the other :nth-* selectors.

CSS Syntax

syntax.css
:nth-last-of-type( <An+B> | even | odd)

Examples: p:nth-last-of-type(1), span:nth-last-of-type(2n), dd:nth-last-of-type(-n+3).

Pattern Values

Pass one pattern; indices count same-tag siblings from the end:

PatternMatches (same type, from end)Try it
1 / :last-of-typeLast element of that typeTry it
2Second-to-last of that typeTry it
-n+3Last three of that typeTry it
odd / evenOdd / even reverse type indicesTry it
3n / n+2Every 3rd from end / all but last of typeAn+B

How to Use :nth-last-of-type()

Pick a tag, count only that tag from the end, then style.

  1. Use mixed content under one parent when you need the “skip other tags” behavior.
  2. Choose the element type (p, span, dd, …).
  3. Add :nth-last-of-type(pattern) with odd, a number, or An+B.
  4. Compare with :nth-last-child() if results look wrong — you may be counting all children by mistake.
PieceRequired?Role
Element typeYesDefines which siblings enter the count
PatternYesodd, even, number, or An+B from the end
Shared parentYesSiblings must share one parent
Other tags—Present but ignored in the type count

Live Preview

Mixed siblings: only span nodes are counted from the end. Other tags stay unmarked.

Rule shown: span:nth-last-of-type(2)

Mixed siblings
span A em (skipped) span B span C strong (skipped) span D

Highlighting the 2nd-to-last span (C)

CSS Examples

Same-type counting from the end, with mixed siblings where it matters. Use Try It Yourself for a live editor.

Example 1 — Second-to-Last of Type

Other tags do not shift the span reverse index.

CSS
<style>
  span:nth-last-of-type(2) {
    background: #bbf7d0;
  }
</style>

<div>
  <span>A</span>
  <em>skip</em>
  <span>B</span>
  <span>C</span>
</div>
Try It Yourself

How It Works

  1. Spans from the end: C=1, B=2, A=3. The em is ignored.
  2. 2 matches B — second-to-last span.
  3. With :nth-last-child(2) you would get a different element (the 2nd child from the end overall).
  4. Use this when mixed markup would break child-based counting.

Example 2 — Last Three Paragraphs

Select the last three p elements among mixed content.

CSS
<style>
  p:nth-last-of-type(-n+3) {
    border-left: 4px solid #2563eb;
  }
</style>

<article>
  <h2>Title</h2>
  <p>One</p>
  <p>Two</p>
  <p>Three</p>
  <p>Four</p>
</article>
Try It Yourself

How It Works

  1. Only p tags count. The h2 does not take a type index.
  2. -n+3 matches the last three paragraphs: Two, Three, Four.
  3. One stays plain — it is the 4th-from-last p.
  4. Handy in articles where headings sit between paragraphs.

Example 3 — Odd from the End

Stripe same-type siblings counted from the last of that type.

CSS
<style>
  p:nth-last-of-type(odd) {
    background: #fef3c7;
  }
</style>

<div>
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
</div>
Try It Yourself

How It Works

  1. From the end among p: 4=odd, 3=even, 2=odd, 1=even.
  2. Paragraphs 4 and 2 get the highlight.
  3. Same idea as :nth-last-child(odd) when every sibling is a p.
  4. Differs when headings or other tags share the parent.

Edge Cases & Pitfalls

Most mix-ups come from confusing type counts with child counts.

vs child

Type ≠ child position

span:nth-last-of-type(2) is not always the same node as span:nth-last-child(2). Use of-type when other tags sit between matches.

tag only

Classes do not define the count

Counting uses the element type. .note:nth-last-of-type(1) still means “last of that tag that also has .note,” not “last .note among classes.”

direction

Same formula, from the end

-n+3 is “first three of type” with :nth-of-type() and “last three of type” here.

shortcut

(1) equals :last-of-type

Prefer :last-of-type when you only need the final same-tag sibling.

Browser Support

:nth-last-of-type() is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:nth-last-of-type()

odd, even, and An+B among same-type siblings from the end have been available across browsers for years (CSS3 Selectors). Internet Explorer 9+ supports this selector.

98% Widely available
Google Chrome Supported
Full support
Mozilla Firefox Supported
Full support
Apple Safari Supported
Full support
Microsoft Edge Supported
Full support
Opera Supported
Full support
Samsung Internet Supported
Full support
Internet Explorer 9+
Supported
:nth-last-of-type() Baseline · Widely available

Bottom line: Safe for everyday same-type reverse patterns in mixed markup. This is not a JavaScript runtime API.

Key Takeaways

  • Role: same-tag siblings, counted from the end (1 = last of that type).
  • Skips other tags in the count — unlike :nth-last-child().
  • Start simple: span:nth-last-of-type(2) { … }.
  • Shortcut: (1) = :last-of-type.

One line: :nth-last-of-type() is reverse same-tag counting — ignore other elements, count from the end.

Frequently Asked Questions

It matches elements by their 1-based position among siblings of the same tag name, counting from the end. Other tag types are skipped in the count.
Yes. Both select the last sibling of that element type. Prefer :last-of-type when you only need that one case.
:nth-last-child() counts every sibling from the end. :nth-last-of-type() counts only matching tags from the end, ignoring other element types in between.
Use p:nth-last-of-type(-n+3). It matches the last, second-to-last, and third-to-last p among the parent's p children.
No. It keys off the element type (tag name), not classes or IDs. Combine with a class if you need both: p.note:nth-last-of-type(1).
No. It is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

Next: :nth-of-type()

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

:nth-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