CSS :nth-last-of-type() Selector

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

What You’ll Learn

The :nth-last-of-type() pseudo-class targets elements by their position among siblings of the same tag type, counting from the end. It combines reverse counting with type-specific matching.

01

By type

Same tag only.

02

Reverse count

1 = last of type.

03

odd / even

From the end.

04

-n+3

Last N of type.

05

vs nth-last-child

Mixed tags.

06

= :last-of-type

Position 1.

Introduction

The :nth-last-of-type() selector in CSS lets you target elements based on their position among siblings of the same element type, counting from the last element of that type backward.

It is especially useful when a parent contains mixed tags — such as paragraphs, headings, and divs — and you need to style the last few paragraphs without being thrown off by other elements that follow.

Definition and Usage

Write element:nth-last-of-type(n) where n is a number, the keyword odd or even, or a formula like 2n+1 or -n+3.

💡
Beginner Tip

Counting starts at 1 from the last element of that type. p:nth-last-of-type(1) is the last paragraph among siblings — even if a <div> comes after it. That is the key difference from :nth-last-child().

📝 Syntax

The syntax for the :nth-last-of-type() pseudo-class is:

syntax.css
element:nth-last-of-type(n) {
  /* CSS properties */
}

element is the tag type you want to match. n can be a number, keyword, or an+b formula.

Common Patterns

ExpressionMatches (3 paragraphs + footer div)
p:nth-last-of-type(1)Third paragraph (last p)
p:nth-last-of-type(2)Second paragraph
p:nth-last-of-type(odd)Third and first paragraphs
p:nth-last-of-type(even)Second paragraph only
li:nth-last-of-type(2n)Every 2nd li from the end
p:nth-last-of-type(-n+2)Last two paragraphs

The an+b Formula (By Type, From the End)

  • n is a counter starting at 0, increasing by 1 for each sibling of the same type from the end.
  • a is the step size — positive steps skip toward earlier siblings of that type; negative steps select the last N of that type.
  • b is the offset — the starting position when counting from the last element of that type.
p:nth-last-of-type(1) li:nth-last-of-type(2) p:nth-last-of-type(-n+3) li:nth-last-of-type(2n)

Related Selectors

  • :nth-of-type() — counts from the first sibling of that type forward
  • :nth-last-child() — counts all sibling children from the end (any tag type)
  • :last-of-type — shorthand for :nth-last-of-type(1)
  • :not() — combine for exclusions like p:not(:nth-last-of-type(1))

⚡ Quick Reference

QuestionAnswer
Selector typeFunctional structural pseudo-class
Syntaxelement:nth-last-of-type(n)
Index starts at1 from the last element of that type
CountsOnly siblings of the same tag type
Common useLast paragraph, last N headings, reverse stripes by type
Browser supportAll modern browsers

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

:nth-last-of-type() is ideal when tag type and reverse position both matter:

  • Article closings — Style the last <p> even when a footer <div> follows.
  • Last N of a typeh2:nth-last-of-type(-n+2) for the final two headings.
  • Navigation listsli:nth-last-of-type(1) for the last menu item of its type.
  • Mixed sibling groups — Ignore <div> or <span> elements between paragraphs.
  • Reverse stripes by typetr:nth-last-of-type(2n) when only table rows of a section should alternate.

👀 Live Preview

The last <p> is bold red and the second-to-last <p> has a green background — the footer <div> does not affect the count:

First paragraph in the section.

Second paragraph in the section.

Third paragraph — last of type.

Examples Gallery

Practice :nth-last-of-type() with list items, mixed-content paragraphs, targeted positions, and last-N patterns.

📜 Core Patterns

Style elements by reverse position among siblings of the same tag type.

Example 1 — Last and second-to-last list items

Style the last <li> blue, the second-to-last green, and make every second <li> from the end bold.

nth-last-of-type-list.css
li:nth-last-of-type(1) {
  background: #dbeafe;
}

li:nth-last-of-type(2) {
  background: #dcfce7;
}

li:nth-last-of-type(2n) {
  font-weight: 700;
}
Try It Yourself

How It Works

Only <li> elements are counted. Item 5 is last of type, Item 4 is second-to-last, and 2n from the end makes Items 4 and 2 bold.

Example 2 — Last paragraph when a footer follows

Highlight the last <p> even though a <div> is the actual last child.

nth-last-of-type-mixed.html
<style>
  p:nth-last-of-type(1) {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<article>
  <p>Intro paragraph.</p>
  <p>Middle paragraph.</p>
  <p>Closing paragraph.</p>
  <div class="footer">Share this article</div>
</article>
Try It Yourself

How It Works

p:nth-last-child(1) would not match here because the footer div is the last child. p:nth-last-of-type(1) correctly targets the closing paragraph.

📄 Targeted Positions

Pinpoint specific reverse positions and select the last N elements of a type.

Example 3 — Highlight the second-to-last paragraph

Use p:nth-last-of-type(2) to style the penultimate paragraph in an article.

nth-last-of-type-second.css
p:nth-last-of-type(2) {
  background: #fef3c7;
  border-left: 4px solid #d97706;
  padding-left: 0.65rem;
}
Try It Yourself

How It Works

Position 2 from the end among <p> siblings is the middle paragraph in a three-paragraph article. Only paragraphs are counted.

Example 4 — Style the last two paragraphs

The -n+2 formula selects the last two elements of a given type.

nth-last-of-type-last-two.css
p:nth-last-of-type(-n+2) {
  background: #eff6ff;
  padding: 0.5rem 0.65rem;
  border-radius: 0.35rem;
}
Try It Yourself

How It Works

When n=0, the formula gives 2. When n=1, it gives 1. The negative step stops after two matches among <p> elements only.

💬 Usage Tips

  • Prefer :last-of-type for one item:nth-last-of-type(1) works, but :last-of-type is more readable.
  • Use -n+k for last N of a typep:nth-last-of-type(-n+3) for the last three paragraphs.
  • Include the element type — Always write p:nth-last-of-type(), not bare :nth-last-of-type().
  • Mixed siblings — This is the go-to selector when other tag types sit between your targets.
  • Combine with parentsarticle > p:nth-last-of-type(1) scopes to direct children.

⚠️ Common Pitfalls

  • Only counts same tag typep:nth-last-of-type(1) ignores <div> siblings but only matches <p> elements.
  • Confusing with nth-last-child — A footer div after paragraphs breaks :nth-last-child() but not :nth-last-of-type().
  • 1-based, not 0-based — The last of type is :nth-last-of-type(1), not 0.
  • Tag must matchdiv:nth-last-of-type(1) will not match a <p> element.
  • Overlapping rules — Multiple :nth-last-of-type() rules can apply to the same element; watch specificity.

♿ Accessibility

  • Contrast on highlights — Emphasized last paragraphs must still meet WCAG contrast requirements.
  • Do not rely on color alone — Pair visual cues with clear headings or labels where meaning matters.
  • Semantic markup — Use proper <p>, <ul>, and heading tags for screen readers.
  • Reading order — Styling the last paragraph does not change document order for assistive technology.

🧠 How :nth-last-of-type() Works

1

Filter by element type

The browser gathers only siblings matching the tag before :nth-last-of-type().

Type filter
2

Number from the end

Those siblings are indexed starting at 1 from the last element of that type.

Reverse index
3

Evaluates n expression

The value inside :nth-last-of-type() is tested against each reverse position.

Match
=

Type-aware reverse styling

Last paragraphs, last list items, and last-N patterns work even with mixed sibling tags.

🖥 Browser Compatibility

:nth-last-of-type() is supported in all modern browsers and has been available since CSS3 Selectors.

Baseline · Universal support

Type-based reverse selectors everywhere

:nth-last-of-type() 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-of-type() pseudo-class 99% supported

Bottom line: :nth-last-of-type() is safe for articles, lists, and mixed-content layouts in all modern projects.

🎉 Conclusion

The CSS :nth-last-of-type() selector provides precise control over elements at the end of a sibling group when tag type matters. It is the reverse, type-aware counterpart to :nth-of-type().

From highlighting the last paragraph in an article to selecting the last two headings with -n+2, mastering :nth-last-of-type() helps you write cleaner stylesheets for real-world mixed HTML.

💡 Best Practices

✅ Do

  • Use -n+k for “last N of this type” patterns
  • Choose nth-last-of-type when mixed tags break nth-last-child
  • Scope with parent selectors like article > p:nth-last-of-type()
  • Use :last-of-type when you only need the final element of a type
  • Test formulas with n=0,1,2 before deploying

❌ Don’t

  • Expect it to count across different tag types
  • Confuse it with nth-last-child on mixed layouts
  • Assume counting starts at 0
  • Omit the element type from the selector
  • Rely on highlight color alone for meaning

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-last-of-type()

Use these points when styling from the end by element type.

5
Core concepts
←1 02

Reverse count

1 = last of type.

Syntax
-n+2 03

Last N of type

Negative step.

Pattern
04

vs nth-last-child

Mixed tags.

Compare
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :nth-last-of-type() pseudo-class matches elements of a specific tag type based on their position among siblings of that same type, counting from the end. You pass a number, odd, even, or an an+b formula inside the parentheses.
Yes. :nth-last-of-type(1) selects the last sibling of that element type, which is identical to :last-of-type. Use whichever reads more clearly in your stylesheet.
:nth-last-child() counts all sibling children from the end regardless of tag type. :nth-last-of-type() counts only siblings of the same element type from the end, ignoring other tags in between.
Use p:nth-last-of-type(-n+3). The negative step selects from the end among p elements only: the last p, second-to-last p, and third-to-last p.
Yes. All modern browsers support :nth-last-of-type(). It has been available since CSS3 Selectors and works reliably for articles, lists, and mixed-content layouts.

Practice in the Live Editor

Open the HTML editor and experiment with :nth-last-of-type(), mixed siblings, and last-N patterns.

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