Styling the Last Element of a List with :last-child
To style only the last element of a list, CSS provides the :last-child pseudo-class. It targets the last child element within its parent container.
:last-child – Selects the last element among its siblings.
Works with any element type, not just list items.
Can be combined with other selectors for more precise targeting.
In this example, only the last <li> (Item 4) receives the light coral background, bold text, and white color, clearly distinguishing it from the other list items.
Use :last-child when you want to target the final element of a container without adding extra classes or IDs.
Combine with other pseudo-classes like :not() or :nth-last-child() for more complex patterns.
Be aware that dynamic changes to the list (adding/removing items) will affect which element is considered the last.
Test across browsers, as :last-child is widely supported but interactions with other pseudo-classes may need verification.
You're given a list of product cards and need to remove the bottom border only from the last one — how would you do that with CSS?
A designer says the last list item in the navigation should have no margin-bottom, but your current CSS isn't working — what's the simplest fix?
If you use :last-child on a list that has a hidden item at the end, will it still style the right element? Why or why not?
Our product list renders dynamically from an API, and sometimes the last item is conditionally hidden — users are seeing broken borders on what looks like the last visible item. How do you fix this?
A teammate used :nth-last-child(1) to style the last list item, but it broke after we added a footer div inside the parent container. What went wrong, and how would you fix it?
We're seeing inconsistent styling on the last list item across browsers — sometimes it works, sometimes it doesn't. What edge cases should you check for?
We have a component library where list items can be any element — divs, spans, anchors — and we need to style the last one consistently without knowing the tag. How do you design this without forcing markup constraints?
Our list component is used in 20+ features, and we're getting inconsistent behavior because some parents have whitespace text nodes. How do you ensure :last-child works reliably across all contexts?
We're migrating from inline styles to CSS modules and need to target the last list item without using global selectors. What's your approach to maintain encapsulation and avoid specificity wars?
We're redesigning our component architecture to support server-side rendering and hydration across multiple frameworks — how do you ensure :last-child styling remains robust when DOM structure is manipulated post-hydration?
Our design system has 50+ list variants, and styling the last item is inconsistent because teams use different wrappers. How do you enforce a scalable, maintainable pattern without forcing every team to follow rigid markup rules?
We're deprecating legacy list components that used JS to add 'last' classes. How do you migrate 300+ usages to pure CSS pseudo-classes without breaking existing layouts or introducing layout shifts?