Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :last-child pseudo-class

Reference for the CSS :last-child pseudo-class: select an element that is the last child of its parent, with syntax, examples, common mistakes and support.

The :last-child pseudo-class matches an element that is the last child of its parent: it has no following sibling element. Text nodes and comments are ignored.

The classic use is removing a bottom border, separator or margin from the final item so a group ends cleanly.

CSS
li:last-child {
  border-bottom: 0;
}

Try it

Separators between items only

Syntax

CSS
:last-child { /* declarations */ }

No arguments; specificity (0,1,0). Matching depends on all sibling elements, regardless of tag.

Syntax and arguments

Part Description
:last-child Matches an element with no following sibling element.
No arguments The pseudo-class is not functional.
Equivalent :nth-last-child(1)
By tag name See :last-of-type.

Usage notes

  • Two conditions again. p:last-child needs a p that is also last. A trailing <script> or <div> after your paragraph stops it matching.
  • Selecting "all but the last". Use li:not(:last-child) to add a separator, margin or comma only between items.
  • Corners on groups. Combine with :first-child to round only the outer corners of a button group or list.
  • Prefer gap. In flex and grid layouts, gap replaces most margin-on-all-but-last patterns.
  • Appending content. Adding an item after the old last one immediately changes which element matches; this can look like a flicker if items are added dynamically.

Browser support

Supported in every browser.