:nth-child() CSS Pseudo Class
Description
The :nth-child pseudo-class in CSS is a structural pseudo-class used to select elements based on their position within a parent element. Unlike classes or IDs, which are explicitly assigned to elements, :nth-child relies on the element’s order among its siblings. This makes it particularly useful for styling lists, tables, or repeated elements without adding extra markup. The syntax for this pseudo-class allows for a wide range of selection patterns, including numeric, formulaic, and keyword-based approaches.
A basic example of :nth-child is selecting every second item in a list:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
li:nth-child(2n) {
background-color: lightblue;
}
In this example, every even <li> element receives a light blue background. The 2n formula here indicates "every second element," while 2n+1 would select all odd elements. You can also use exact numbers, like li:nth-child(3) to select only the third child.
The :nth-child pseudo-class can be combined with other selectors for more precise targeting. For instance, if you want to style only the <li> elements that are direct children of a specific <ul>:
ul > li:nth-child(3n) {
font-weight: bold;
}
Here, every third child <li> of the <ul> is bolded. This works seamlessly with other CSS properties, such as color, background-color, or margin.
An important nuance is that :nth-child counts elements of all types within the parent unless combined with a type selector. For example, p:nth-child(2) selects a <p> element only if it is the second child of its parent, not just the second <p>. If you want to select every second <p> regardless of other elements, you might need :nth-of-type.
Another handy use of :nth-child is alternating row colors in tables:
<table>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
<tr><td>Row 4</td></tr>
</table>
tr:nth-child(even) {
background-color: #f2f2f2;
}
This gives a striped table effect, improving readability. You can also combine it with pseudo-classes like :hover to create dynamic hover effects on alternate rows.
Overall, :nth-child is a powerful, flexible pseudo-class that allows developers to target elements with precision based purely on their position, reducing the need for extra classes or JavaScript for styling purposes.
Interactive Demo
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Syntax
:nth-child(odd | even | <integer> | <an-plus-b> [of <complex-selector-list>]?) {
/* ... */
}Values
- oddMatches elements whose position (counted from the first child) is odd: 1, 3, 5 and so on. Equivalent to
2n+1. - evenMatches elements whose position (counted from the first child) is even: 2, 4, 6 and so on. Equivalent to
2n. - <integer>A single position, counted from 1. For example
3matches the third child. - <an-plus-b>A pattern written as
An+B, whereAandBare integers andntakes the values 0, 1, 2 and so on. Every element whose position equalsAn+Bfor some value ofnis matched.AorBcan be zero or negative:3nmatches every third element,-n+3matches the first three. Only positive results count, because positions start at 1. - of <complex-selector-list>Optional. When present, only siblings that match the selector list are counted, so
:nth-child(2 of .item)matches the second.item, ignoring other siblings.
Example
Browser Support
The following information will show you the current browser support for the CSS :nth-child() pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.
Desktop





Tablets & Mobile





