CSS :last-child Selector

The :last-child CSS pseudo-class represents the last element among a group of sibling elements.

This CSS tutorial explains how to use the CSS selector called :last-child with syntax and examples.

The CSS :last-child selector allows you to target an element that is the last child element within its parent.

element:last-child { style_properties }

The :nth-last-child selector allows you select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements. It functions the same as :nth-child except it selects items starting at the bottom of the source order, not the top.

Suppose we have a list with an unknown number of items, and we wish to highlight the second-to-last item

The :last-child selector matches every element that is the last child of its parent.

Example -