Apart from the first and last child,
you can also select a specific child
element using the :nth-child
pseudo-class:
Here :nth-child(2)
selects the 2nd child.
Try changing the number to 1, 3 or 4
to see a different child being selected.
The child count starts
from 1. Hence :nth-child(1)
is
same as :first-child
.
Using the nth-child
pseudo-class, you can also select
even or odd numbered child
elements, as shown below.
Try changing odd
to even
in
the following example:
Try all these values in the
example that follows:
:nth-child(n+3)
- Selects all matching child elements starting from 3rd one
:nth-child(3n)
- Selects every 3rd matching child element
:nth-child(3n-2)
- Selects every 3rd matching child element starting from the first one