CSS Reference Pseudo-class

:nth-child() 4e3p38

The :nth-child() is a CSS pseudo-class selector that allows you to select elements based on their index (source order) inside their container. 3d92u

You can in a positive number as an argument to :nth-child(), which will select the one element whose index inside its parent matches the argument of :nth-child(). For example, li:nth-child(3) will select the list item with an index value 3; that is, it will select the third list item.

You can also in one of two predefined keywords: even and odd. li:nth-child(even) will select all list items with even index numbers (2, 4, 6, 8, etc.), and li:nth-child(odd) will select all list items with odd index numbers (1, 3, 5, 7, etc.).

:nth-child() also allows you to select one or more elements using a formula (or an equation)—an+b—that is ed to it as an argument. The syntax is :nth-child(an+b), where you replace a and b with integer values of your own so that, after n is substituted with positive numbers (0, 1, 2, 3, etc.), the resulting number is the index of the element you want to select. For example, :nth-child(3n+1) will select the 1st (3*0 +1 = 1) child, 4th (3*1 +1 = 4) child, 7th (3*2 +1 = 7) child, etc.and so on.

What :nth-child(an+b) does is it divides the container’s children into a elements (the last group taking the remainder), and then selects the bth element of each group. So, li:nth-child(3n+1) will divide the list items into 3 groups, put the remainder items in a fourth group (if the number of items is not divisible by 3), and then it will match the first item in each group.

For example, the following image shows the result of selecting li:nth-child(3n+1) in a list of items. There are 10 items in the list, and we have a = 3, so the 10 items will be divided by 3. 10/3 = 3 + 1, so there will be one item remaining which will be grouped into a last group of its own. Then, for each of the four groups, the first item will be selected. In the following image, the matching items have a khaki background.

nth-child-example
The result of applying :nth-child(3n+1) on a list of items. The items with a blue background color are the ones that are selected by :nth-child(3n+1). Items are divided into 3 groups (black border), and you can see how the first item in each group is matched, including the additional group with the remainder items.

When you a formula to :nth-child(), the browser will iterate through all children of the container and then select the ones that match the formula. The list of items are treated as items of an array, where each item has an index that may or may not match the result of the formula.

Dealing with the calculations for :nth-child can be confusing and daunting. In addition to that, being able to visualize the results and select items visually is usually a lot easier than having to do the math ourselves. Because of that, some really nice tools have been developed that help you visualize :nth-child. The following are useful tools for that:

Just like other pseudo-class selectors, the :nth-child() selector can be chained with other selectors such as ::after, among others. For example, the following rule will provide hover styles for elements matching the formula in :nth-child():

li:nth-child(2n+3)::after {
    /* styles here */
}

tr:nth-child(even):hover {
    background-color: lightblue;
}
                

There is a pseudo-class selector that has a similar functionality to that of :nth-child(), that selector is the :nth-last-child() is similar to :nth-child, except that instead of iterating through the elements from the first one downwards, it starts iterating from the last element up.

:nth-child() is also similar to :nth-of-type() entry.

Examples 3e5p5z

The following example selects the even rows in a table and applies a background color to them to create a zebra effect.

tr:nth-child(even) {
    background-color: #eee;
}
                

The following are valid :nth-child() declarations:

li:nth-child(-n+1) {}

p:nth-child(odd) {}

tr:nth-child(2n) {} /* 2n is equivalent to "even" */

article:nth-child(2n+1) {} /* 2n+1 is equivalent to "odd" */

li:nth-child(4n+1) {}
                

Live Demo 2x4j2k

Change the number of list items and the arguments of :nth-child() in the following demo to see how the matching items change.

View this demo on the Codrops Playground

Browser k5t66

The :nth-child() selector is ed in Chrome, Firefox, Safari, Opera 9.5+, Internet Explorer 9+, and on Android and iOS.

Further Reading 4g1p1d

Written by

Last updated June 11, 2020 at 9:49 pm by Mary Lou

Do you have a suggestion, question or want to contribute? Submit an issue.