CSS :last-child Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Structural Selectors

What You’ll Learn

The :last-child pseudo-class matches an element only when it is the very last child inside its parent. It is essential for list dividers, menu styling, and bottom spacing fixes.

01

Last child

No sibling after.

02

Any type

Tag agnostic.

03

Lists

Style last item.

04

Spacing

Remove bottom gap.

05

Combine

div > p:last-child.

06

vs :last-of-type

Know the diff.

Introduction

The :last-child selector in CSS is a structural pseudo-class used to match an element that is the last child of its parent container.

It lets you apply unique styles based on position in the DOM tree — for example, highlighting the final item in a list or removing extra bottom margin from the last paragraph in a section.

Definition and Usage

Write the element selector before the pseudo-class: li:last-child or p:last-child. The rule matches only when that element is the last child among all siblings inside its parent.

💡
Beginner Tip

:last-child cares about overall position, not tag type. If a <div> comes after your <p>, then p:last-child will not match. Use :last-of-type when you need the last paragraph regardless of what comes after it.

📝 Syntax

The syntax for the :last-child pseudo-class is:

syntax.css
element:last-child {
  /* CSS properties */
}

Here, element is the tag or selector for the child you want to style. The element must also be the last child of its parent to match.

Basic Example

last-child-list.css
li:last-child {
  color: #1d4ed8;
  font-weight: 700;
}
li:last-child p:last-child .card:last-child *:last-child

Syntax Rules

  • The matched element must be the last child of its parent, regardless of tag type.
  • Combine with combinators: div > p:last-child for direct children only.
  • Any following sibling — even a different tag — prevents a match.
  • Use *:last-child to target whatever element is last inside a container.
  • Do not confuse with :last-of-type, which ignores other tag types.

:last-child vs :last-of-type

SelectorMatches when…
p:last-childThe <p> is the last child of its parent (no element after it)
p:last-of-typeThe <p> is the last <p> among siblings, even if a <div> comes after it

Related Selectors

  • :first-child — matches the first child of a parent
  • :nth-child() — matches children by index (1-based)
  • :last-of-type — last element of a given tag among siblings
  • > child combinator — limit matches to direct children

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
When it appliesElement is the last child of its parent
Index basisDOM order among all sibling children
Common useLast list item, remove bottom margin, drop final border
Typical pattern.content > *:last-child { margin-bottom: 0; }
Browser supportAll modern browsers

When to Use :last-child

:last-child is ideal for position-based styling at the end of a group:

  • Navigation lists — Remove the border or divider after the final menu item.
  • Stacked content — Remove bottom margin from the last paragraph in an article block.
  • Spacing resets — Strip extra padding below the last child in a card or panel.
  • Card grids — Style the final card in a row when markup order is predictable.
  • Table rows — Round corners or remove border on the last row in a tbody.

👀 Live Preview

The last list item matches li:last-child and gets blue bold styling:

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

Examples Gallery

Practice :last-child with lists, spacing, scoped paragraphs, and comparison with :last-of-type.

📜 Core Patterns

Style the last child element inside a parent container.

Example 1 — Style the last list item

Make the final <li> in a list stand out with bold blue text.

last-child-list.html
<style>
  li:last-child {
    color: #1d4ed8;
    font-weight: 700;
  }
</style>

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

How It Works

Only the fourth <li> has no sibling after it, so it is the last child of the <ul> and receives the styles.

Example 2 — Remove bottom margin from the last child

A common layout fix: strip extra space below the last element in a content block.

last-child-margin.css
div p:last-child {
  margin-bottom: 0;
}

div p {
  margin-bottom: 1rem;
}
Try It Yourself

How It Works

All paragraphs get bottom margin, but p:last-child resets it to zero for whichever paragraph is truly last inside the div.

📄 Specificity & Comparison

Combine with element selectors and understand when :last-child fails.

Example 3 — Remove border from the last list item

A classic menu pattern: add dividers between items, then drop the border on the final one.

last-child-border.css
li {
  border-bottom: 1px solid #e2e8f0;
  padding: 0.65rem 0.9rem;
}

li:last-child {
  border-bottom: none;
}
Try It Yourself

How It Works

Every list item gets a bottom border except the last one, which matches li:last-child and has its border removed.

Example 4 — :last-child vs :last-of-type

See why p:last-child fails when a <div> is last, while p:last-of-type still matches.

last-child-vs-type.css
/* Only matches if p is the last child */
p:last-child {
  border-right: 4px solid #ef4444;
  padding-right: 0.75rem;
}

/* Matches the last p among siblings */
p:last-of-type {
  border-right: 4px solid #2563eb;
  padding-right: 0.75rem;
}
Try It Yourself

How It Works

Because a <div> is the last child, p:last-child does not match. p:last-of-type ignores the div and styles the last paragraph.

💬 Usage Tips

  • Any element type — Apply :last-child to paragraphs, divs, table rows, or list items.
  • Combine selectors — Use div p:last-child to target only the last paragraph inside a div.
  • Border cleanup — Pair with border-bottom on all items and :last-child { border-bottom: none; }.
  • Spacing stacks — Reset margin-bottom or padding-bottom on the final child in a group.
  • When type matters — Switch to :last-of-type if other tags may follow your target element.

⚠️ Common Pitfalls

  • Expecting last of a type:last-child targets the last child of any type. Use :last-of-type for the last element of a specific tag.
  • Hidden trailing elements — An invisible or empty element after your target still counts as a following sibling.
  • Script or style tags — In some legacy markup, trailing elements can block :last-child from matching your intended node.
  • Dynamic content — JavaScript-appended elements at the end can change which node is :last-child.
  • Wrong syntax — The selector before :last-child is the child element, not the parent. Use ul > li:last-child for context.

♿ Accessibility

  • Do not rely on position alone — Visual emphasis on the last item should not be the only way to convey importance.
  • Lists remain semantic — Use proper <ul> or <ol> markup; styling the last item does not replace list structure for screen readers.
  • Focus order — Position styling does not change tab order; keep interactive elements logically ordered.
  • Color contrast — Ensure last-child highlight colors meet WCAG contrast requirements.

🧠 How :last-child Works

1

Browser finds candidate elements

CSS gathers elements matching the tag or selector before :last-child.

Match
2

Checks sibling position

For each candidate, the browser asks: is this the last child of its parent?

Position
3

Styles apply to matches

Only elements with no following sibling children receive the rule.

Render
=

Position-aware styling

The last child gets clean borders, spacing, or emphasis in lists and layouts.

🖥 Browser Compatibility

The :last-child pseudo-class is supported in all modern browsers and has been available since CSS2.

Baseline · Universal support

Structural selectors everywhere

: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
:last-child pseudo-class 99% supported

Bottom line: :last-child is safe for lists, menus, and layout spacing in all modern projects.

🎉 Conclusion

The :last-child pseudo-class is a valuable tool for targeting the last element in a group without modifying the HTML structure. It simplifies styling and enhances flexibility for lists, menus, and layout stacks.

Remember that it checks overall sibling order, not tag type. When you need the last element of a specific tag, reach for :last-of-type instead.

💡 Best Practices

✅ Do

  • Use li:last-child { border-bottom: none; } for clean menu dividers
  • Reset spacing with *:last-child { margin-bottom: 0; }
  • Inspect DOM order when styles do not apply
  • Combine with child combinator > for direct children
  • Choose :last-of-type when tag type matters

❌ Don’t

  • Assume the last <p> is always :last-child
  • Forget hidden or wrapper elements after your target
  • Confuse the child selector with the parent selector
  • Rely on position alone for meaning or hierarchy
  • Overuse when a class on the last item is clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about :last-child

Use these points when styling by child position.

5
Core concepts
📝 02

Syntax

li:last-child.

Pattern
📄 03

Lists

Clean last item.

Use case
04

vs last-of-type

Different rules.

Compare
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :last-child pseudo-class matches an element only when it is the very last child of its parent — no other element sibling comes after it.
Write the element selector before the pseudo-class: li:last-child matches an li that is the last child of its parent. It does not mean "the last li among siblings" unless that li is also the last child overall.
:last-child requires the element to be the last child of any type. :last-of-type matches the last element of that tag name among siblings, even if another element type comes after it.
Usually because another element appears after the target — even a hidden element, comment sibling, or a different tag like a script or div after your li. Check the actual DOM order inside the parent.
Yes. All modern browsers support :last-child. It has been available since CSS2 and works reliably for lists, menus, and layout spacing.

Practice in the Live Editor

Open the HTML editor and experiment with :last-child, lists, and spacing 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