:nth-child()
W3C CSS Snapshot 2026
Match elements by position among siblings, counting from 1.
On this page
Syntax and values
| Value or syntax | What it does |
|---|---|
:nth-child(2) | The second element child. |
:nth-child(even) | Positions 2, 4, 6, and so on. |
:nth-child(odd) | Positions 1, 3, 5, and so on. |
:nth-child(3n + 1) | Positions 1, 4, 7, and so on. |
:nth-child(2 of .task) | The second sibling in the subset that matches .task. |
Try the example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>:nth-child()</title>
<style>
li:nth-child(even) { background: #def5f5; }
</style>
</head>
<body>
<ul>
<li>Check in</li>
<li>Pick up materials</li>
<li>Find a seat</li>
<li>Start the activity</li>
</ul>
</body>
</html>
What to expect Items 2 and 4 are highlighted. Change even to odd and compare.