Favorites

:nth-of-type() 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-of-type()
CSS Pseudo ClassThis pseudo class is supported by all modern browsers.
Updated 22nd November 2026

Description

The :nth-of-type pseudo-class in CSS is a powerful selector used to target one or more elements based on their position among sibling elements of the same type. Unlike the :nth-child pseudo-class, which counts all child elements regardless of type, :nth-of-type specifically considers the element’s type, making it highly useful when you need precise styling for elements like multiple li items, p paragraphs, or div blocks within a container. It accepts various formula patterns, including keywords, integers, and mathematical expressions, to select elements at specific intervals or positions.

The syntax of :nth-of-type allows several forms of argument: a single number (e.g., :nth-of-type(3)) selects the third occurrence of an element type, the keyword odd or even selects elements at odd or even positions, and the functional notation An+B (for example, :nth-of-type(2n+1)) allows selection of every second element plus one offset, providing a flexible way to style repeating patterns. This flexibility makes it ideal for tasks such as alternating row colors in tables, creating staggered layouts, or applying animations to every third element in a list.

In practical usage, :nth-of-type can be combined with other selectors to achieve complex styling. For instance, if you have a list of li items inside an unordered list and want to highlight every third item:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
</ul>
li:nth-of-type(3n) {
  background-color: #f0f0f0;
}

In this example, the third and sixth li elements will have a light gray background. Additionally, :nth-of-type can be used with pseudo-classes like :first-of-type or combined with pseudo-elements such as ::before to create more intricate designs, like custom bullet decorations or numbered sequences. Understanding and leveraging :nth-of-type can significantly enhance your ability to apply precise, pattern-based styles without adding extra classes or modifying HTML structure.

Interactive Demo

p 1

span 1

p 2

p 3

span 2

p 4

span 3

p 5

span 4
Demo Code
HTML Code
<div class="demo-container">
	<div class="demo-element"><div class="demo-types">
	<p>p 1</p><span>span 1</span><p>p 2</p><p>p 3</p><span>span 2</span><p>p 4</p><span>span 3</span><p>p 5</p><span>span 4</span>
</div></div>
</div>
CSS Code
.demo-types {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	width: 300px;
}
.demo-types > * {
	margin: 0;
	padding: 6px 8px;
	font-size: 13px;
	border: 2px solid #4361ee;
	background: #e7ecff;
}
.demo-types > span {
	border-color: #2a9d8f;
	background: #e9f5f3;
	border-radius: 12px;
}
p:nth-of-type(2) {
	background: #f72585;
	color: #fff;
	outline: 2px solid #f72585;
}

Syntax

:nth-of-type(odd | even | <integer> | <an-plus-b>) {
  /* ... */
}

Values

  • oddMatches elements whose position (among siblings of the same type, counted from the first) is odd: 1, 3, 5 and so on. Equivalent to 2n+1.
  • evenMatches elements whose position (among siblings of the same type, counted from the first) is even: 2, 4, 6 and so on. Equivalent to 2n.
  • <integer>A single position, counted from 1. For example 3 matches the third element of its type.
  • <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.

Example

<div class="container">
<h2>Fruit List</h2>
<p>Apples</p> <h2>Vegetable List</h2>
<p>Carrots</p> <p>Potatoes</p> <p>Bananas</p> </div>
/* Selects the 2nd 

element specifically */
p:nth-of-type(2) {
background-color: #ffd700;
font-weight: bold;
padding: 5px;
}

/* Selects every even

element (2nd, 4th, 6th...) */
p:nth-of-type(even) {
border-left: 5px solid #ff4500;
}

Open in editor

Browser Support

The following information will show you the current browser support for the CSS :nth-of-type() 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