CSS3 :nth-last-child() Selector
Complete CSS Selectors Reference
Example
Specify the background color for each p element that matches the second-to-last sibling element of its type:
Definition and Usage
The :nth-last-child(n) selector matches every element that is the nth child, regardless of type, of its parent, starting from the last child.
n can be a number, a keyword, or a formula.
Tip: See the :nth-last-of-type() selector. This selector matches the nth child of a particular type, starting from the last child.
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
| Selector | |||||
|---|---|---|---|---|---|
:nth-last-child() |
4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
More Examples
Example 1
Odd and even are keywords that can be used to match child elements whose index is odd or even.
Here, we specify two different background colors for odd and even p elements in reverse order:
Example 2
Using the formula (an+ b). Description: a represents a cycle size, n is a counter (starting at 0), and b is an offset.
Here, we specify the background color for all p elements whose index is a multiple of 3 in reverse order: