CSS :nth-last-child() Selector

Selects all elements that are the nth-child of their parent, counting from the last element to the first.

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.

The :nth-last-child() CSS pseudo-class matches one or more elements based on their position among a group of siblings, counting from the end.

Suppose we have a list with an unknown number of items, and we wish to highlight the second-to-last item (in this exact example, the "Fourth Item"):

The :nth-last-child(n) selector matches every element that is the nth child, regardless of type, of its parent, counting from the last child.n can be a number, a keyword, or a formula.

Example -