CSS3 :nth-last-of-type()Selectors

Complete CSS Selector Reference Manual

Example

Specifies the background color for each p element that matches the 2nd-to-last sibling element of the same type:

p:nth-last-of-type(2)
{
background:#ff0000;
}

Try it Yourself »

Definition and Usage

:nth-last-of-type(n) selector matches the nth-to-last sibling element of the same type.

nIt can be a number, a keyword, or a formula.

Tip:Please refer to::nth-last-child()Selector. This selector matches the nth-to-last structural child element within the parent element.


Browser Support

The numbers in the table indicate the first browser version that supports this property.

Selectors
:nth-last-of-type() 4.0 9.0 3.5 3.2 9.6

Examples

More Examples


Example 1

Odd and even can be used as keywords to match child elements whose index is odd or even.

Here, we specify two different background colors for odd and even nth-last p elements:

p:nth-last-of-type(odd)
{
background:#ff0000;
}
p:nth-last-of-type(even)
{
background:#0000ff;
}

Try it Yourself »

Example 2

Using the formula (an + b). Description: a represents the size of a cycle, n is a counter (starting from 0), and b is an offset.

Here, we specify a background color for all p elements whose index, in reverse order, is a multiple of 3:

p:nth-last-of-type(3n+0)
{
background:#ff0000;
}

Try it Yourself »


Complete CSS Selector Reference Manual

other extensions