Favorites

:nth-child() CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:nth-child()
CSS Pseudo ClassThis pseudo class is supported by all modern browsers.
Updated 22nd November 2026

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
Dashed items have class="demo-sale".
Demo Code
HTML Code
<div class="demo-container">
	<div class="demo-element"><ul class="demo-list">
	<li>1</li>
	<li class="demo-sale">2</li>
	<li>3</li>
	<li>4</li>
	<li class="demo-sale">5</li>
	<li>6</li>
	<li class="demo-sale">7</li>
	<li>8</li>
	<li class="demo-sale">9</li>
	<li>10</li>
</ul>
<div class="demo-note">Dashed items have class="demo-sale".</div></div>
</div>
CSS Code
.demo-list {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	width: 300px;
	margin: 0;
	padding: 0;
	list-style: none;
}
.demo-list li {
	width: 52px;
	padding: 8px 0;
	text-align: center;
	background: #e7ecff;
	border: 2px solid #4361ee;
	font-size: 14px;
}
.demo-list .demo-sale {
	border-style: dashed;
}
.demo-note {
	margin: 6px 0 0;
	font-size: 12px;
	color: #666;
}
li:nth-child(3) {
	background: #f72585;
	color: #fff;
	outline: 2px solid #f72585;
}

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 3 matches the third child.
  • <an-plus-b>A pattern written as An+B, where A and B are integers and n takes the values 0, 1, 2 and so on. Every element whose position equals An+B for some value of n is matched. A or B can be zero or negative: 3n matches every third element, -n+3 matches 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

<ul class="item-list">
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
<li>Fourth Item</li>
<li>Fifth Item</li>
<li>Sixth Item</li>
</ul>
/* 1. Select exactly the second item */
.item-list li:nth-child(2) {
background-color: #ffd700;
font-weight: bold;
}

/* 2. Select every "even" item (2, 4, 6...) */
/* This is great for zebra-striping tables or lists */
.item-list li:nth-child(even) {
color: #2c3e50;
border-left: 5px solid #3498db;
}

/* 3. Using a formula: 3n (Every 3rd item) */
/* This selects 3, 6, 9, etc. */
.item-list li:nth-child(3n) {
text-decoration: underline;
}
Open in editor

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.

This pseudo class is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee