Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

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

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

Complete CSS SelectorComplete CSS Selector Reference Manual

Example

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

p:nth-last-of-type(2)

{

background:#ff0000;

}

Try it »


Definition and Usage

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

n It can be a number, a keyword, or a formula.

**Tip:** See::nth-last-child()selector. This selector matches the nth structural child from the end within the parent


Browser support

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

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

More examples

Example 1

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

Here we assign two different background colors to odd and even reversed-order p elements:

p:nth-last-of-type(odd)

{

background:#ff0000;

}

p:nth-last-of-type(even)

{

background:#0000ff;

}

Try it »

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 the offset.

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

p:nth-last-of-type(3n+0)

{

background:#ff0000;

}

Try it »


Complete CSS SelectorComplete CSS Selector Reference Manual