29 May 2023
The :nth-child()
is a pseudo-class that matches elements based on their position in a group of siblings.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
li:nth-child(2) {
color: grey;
}
Result
# select odd elements
:nth-child(odd)
# select even elements
:nth-child(even)
# select every 5th element
:nth-child(5n)