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.
Fundamentals
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.
Foundation
📝 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.
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;}
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.
Compatibility
🖥 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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:last-child pseudo-class99% supported
Bottom line::last-child is safe for lists, menus, and layout spacing in all modern projects.
Wrap Up
🎉 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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :last-child
Use these points when styling by child position.
5
Core concepts
Last01
Last child
No sibling after.
Purpose
📝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.