CSS :last-child Pseudo-class

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

Definition and Usage

The :last-child pseudo-class matches an element that is the last element child of its parent.

Example: li:last-child styles a list item only when that li is the last child of its parent. It is the same match as :nth-last-child(1). For “last of this tag,” use :last-of-type.

Baseline · Widely available.

Not deprecated. A long-standing structural pseudo-class from Selectors Level 3 / 4.

CSS Syntax

syntax.css
:last-child { /* or */ E:last-child { … }

Example: li:last-child { border: 2px solid orange; }

Selector Forms

FormDescriptionTry it
:last-childAny element that is the last child of its parentTry it
E:last-childElement of type E that is also the last childTry it
vs :last-of-typeLast of that tag vs must be last child overallTry it
Nested listsEach parent has its own last childTry it
Spacing trimRemove margin on the last item in a stackTry it
MenusDrop the last divider in a horizontal navTry it

How to Use :last-child

Confirm the target is truly the last element child, then add the pseudo-class.

  1. Look at the parent’s children in the DOM (DevTools helps).
  2. Write E:last-child when you care about both the tag and last position.
  3. Style borders, spacing, or emphasis for that last item.
  4. If another tag can appear after it, switch to :last-of-type.
PieceRequired?Role
Parent with childrenYesSiblings are checked relative to one parent
:last-childYesMatches only the last element child
Type selectorOptionalLimits the match to a tag (e.g. li)

Live Preview

The orange border marks .row:last-child. Append a note after the rows to see the highlight leave Item 3.

Sibling demo

When a note becomes the last child, no .row matches .row:last-child.

Siblings
Item 1
Item 2
Item 3

Item 3 matches .row:last-child.

CSS Examples

Each snippet focuses on :last-child. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Style the Last List Item

Highlight the last li in a list.

CSS
<style>
  li:last-child {
    border: 2px solid orange;
  }
</style>

<ul>
  <li>Adhemar da Silva</li>
  <li>Wang Junxia</li>
  <li>Wilma Rudolph</li>
</ul>
Try It Yourself

How It Works

  1. Pick the tag. li:last-child only considers list items.
  2. Require last place. The li must also be the parent’s last element child.
  3. Style that item. An orange border marks the match.
  4. Others stay plain. Earlier items are not last children.

Example 2 — When the Last Child Is Not a p

p:last-child fails if another element comes after it.

CSS
<style>
  p:last-child {
    color: #16a34a;
    font-weight: 700;
  }
</style>

<div>
  <p>This text isn't selected.</p>
  <p>This text is selected!</p>
</div>

<div>
  <p>This text isn't selected.</p>
  <h2>Not a p — so no p:last-child here.</h2>
</div>
Try It Yourself

How It Works

  1. First box. The last child is a p, so p:last-child matches.
  2. Second box. The last child is an h2.
  3. No match. The preceding p is not the last child.
  4. Need last p? Use p:last-of-type instead.

Example 3 — Nested Lists

Each list has its own last child.

CSS
<style>
  ul li {
    color: #2563eb;
  }

  ul li:last-child {
    color: #dc2626;
    font-weight: 700;
  }
</style>

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3
    <ul>
      <li>Item 3.1</li>
      <li>Item 3.2</li>
      <li>Item 3.3</li>
    </ul>
  </li>
</ul>
Try It Yourself

How It Works

  1. Outer list. Item 3 is the last child of the outer ul.
  2. Inner list. Item 3.3 is the last child of the nested ul.
  3. Same selector. ul li:last-child can match in every list.
  4. Independent parents. Last-child is always relative to one parent.

Edge Cases & Pitfalls

Most surprises come from DOM order, not from the selector itself.

order

Another element after the target

If an h2, script, or any element comes after, p:last-child will not match that p.

vs type

Not the same as :last-of-type

Need the last p even when a footer follows? Use :last-of-type.

text

Whitespace does not block it

In modern CSS, trailing text nodes or spaces between tags do not stop :last-child. Later elements do.

alias

Same as :nth-last-child(1)

:last-child and :nth-last-child(1) select the same elements.

only

Single child also matches

An only child is still the last child. For “exactly one child,” use :only-child.

Browser Support

:last-child is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:last-child

Supported in all modern browsers (Chrome 1+, Firefox 1+, Safari 3.2+, Edge 12+, Opera 9.5+, IE 9+). Selectors Level 4 “no parent required” is a later refinement.

97% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 3.2+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support
:last-child Widely supported

Bottom line: Core matching is safe for modern sites. It is not a JavaScript runtime API.

Key Takeaways

  • Role: match the last element child of a parent.
  • Form: E:last-child = tag E and last position.
  • vs :last-of-type: use of-type when another tag can come after.
  • Alias: same match as :nth-last-child(1).

One line: :last-child styles an element only when it is the last element sibling under its parent.

Frequently Asked Questions

It matches an element that is the last element child of its parent. No other element sibling may come after it.
Use :last-child alone or with a type, such as li:last-child. That means “an li that is also the last child,” not “the last li before another tag.”
:last-child requires the element to be the last child of any type. :last-of-type matches the last element of that tag among siblings, even if another tag comes after it.
Usually another element sits after the target (for example a script, footer, or div after your last li). Whitespace text nodes do not block :last-child in modern CSS—only later element siblings do.
Yes. :last-child matches the same elements as :nth-last-child(1).
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: :last-of-type

Style the last element of a given tag among siblings, even when other tags follow.

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

8 people found this page helpful