CSS Portal

:nth-of-type CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

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.

Syntax

element: nth-of-type (odd | even | <number> | <expression>) {
  /* ... */
}

Values

  • oddAll odd item numbers.
  • evenAll even item numbers.
  • <number>The serial number of the child relative to its parent. Numbering starts with 1, this will be the first item in the list.
  • <expression>It is given in the form an ± b , where a and b are integers, and n is a counter that automatically takes the value 0, 1, 2 ...
If a is zero, then it is not written and the record is reduced to b. If b is zero, then it is also not specified and the expression is written in the form an. a and b can be negative numbers, in which case the plus sign changes to minus, for example: 5n-1.
By using negative values ​​of a and b, some results can also turn out to be negative or equal to zero. However, elements are affected only by positive values ​​due to the numbering of elements starting 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;
}

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 psuedo class.

This psuedo 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
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!