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

CSS3 :nth-last-child() selector

CSS3 :nth-last-child() 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-child

(2)

{

background:

#ff0000

;

}

Try it »


Definition and Usage

:nth-last-child(n) selector matches every element that is the Nth child of its parent, regardless of type, counting from the last child.

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

Tip: See:nth-last-of-type() 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-child() 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-child(odd)

{

background:#ff0000;

}

p:nth-last-child(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-child(3n+0)

{

background:#ff0000;

}

Try it »


Complete CSS SelectorComplete CSS Selector Reference Manual