
The :nth-child() CSS pseudo-class allow us to select elements based on their position, precisely targeting specific elements within a document tree. Let’s dive into nth-child CSS and explore the examples!
Sample HTML:
<ol>
<li class="example3">Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem ipsum dolor sit amet.</li>
</ol>
1. nth-child(n)
In this example, the nth-child(4) is specifically target the 4th item of the <li> element.
CSS Syntax:
li:nth-child(4) {
background: yellowgreen;
}
Result:
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
2. Odd/ Even
In this example, we use nth-child(even) to target all of the even rows and assign the background color “lightyellow”. We also use nth-child(odd) to target the odd rows and assign “aqua” to the background color.
CSS Syntax:
li:nth-child(even) {
background: lightyellow;
}
li:nth-child(odd){
background: aqua;
}
Result:
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
3. (Xn + Y)
CSS Syntax:
li:nth-child(3n + 1){
background: pink;
}
In this example, (3n + 1) is equivalent to :
(3 x 0) + 1 = 1 = 1st Element
(3 x 1) + 1 = 4 = 4th Element
(3 x 2) + 1 = 7 = 7th Element
Note: both X & Y must be in integer value.
Result:
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
4. The “of” syntax
CSS Syntax:
li:nth-child(-n + 2 of .example3){
background:orange;
}
In this example, (-n + 2) is equivalent to :
(-1 x 0) + 2 = 2 = 2nd Element
(-1 x 1) + 2 = 1 = 1st Element
(-1 x 2) + 2 = 0 = 0th Element
Hence, -n + 2 will target the first 2 elements,
with the presence of the “of .example3” syntax in the CSS, the code will target the first 2 elements which contain the “.example3” class only. Therefore, in this example, only the first row will be highlighted in orange.
Result:
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
- Lorem ipsum dolor sit amet.
The ‘of’ syntax helps resolve hidden row issues in odd/even scenarios
The common issue in Striped Table:
Example HTML:
<ul>
<li class="skip" style="display: none;">Item 1.</li>
<li>Item 2.</li>
<li>Item 3.</li>
<li>Item 4.</li>
<li class="skip" style="display: none;">Item 5.</li>
<li>Item 6.</li>
<li>Item 7.</li>
<li>Item 8.</li>
</ul>
CSS:
li:nth-child(odd){
background:yellowgreen;
}
Problem
In below example, only li:nth-child(odd) is used. However, when some rows are hidden, such as in this example, where the 1st and 5th rows have been hidden and still counted as odd; although technically they are, but visually they are not. Hence, you will notice some UI discrepancies, as shown in the example below.
- Item 2.
- Item 3.
- Item 4.
- Item 6.
- Item 7.
- Item 8.
Solution
However, using the “of” selector and the “:not(.skip)” pseudo-class, we can target only the <li> elements that are not hidden.
CSS:
li:nth-child(odd of :not(.skip)){
background:yellowgreen;
}
Result:
- Item 2.
- Item 3.
- Item 4.
- Item 6.
- Item 7.
- Item 8.
Happy Coding! ![]()



Leave a comment