everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

2024 Guide to CSS nth-child: Target Any Element Like a Ninja! (Examples Included)

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:

  1. Lorem ipsum dolor sit amet.
  2. Lorem ipsum dolor sit amet.
  3. Lorem ipsum dolor sit amet.
  4. Lorem ipsum dolor sit amet.
  5. Lorem ipsum dolor sit amet.
  6. Lorem ipsum dolor sit amet.
  7. Lorem ipsum dolor sit amet.
  8. 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:

  1. Lorem ipsum dolor sit amet.
  2. Lorem ipsum dolor sit amet.
  3. Lorem ipsum dolor sit amet.
  4. Lorem ipsum dolor sit amet.
  5. Lorem ipsum dolor sit amet.
  6. Lorem ipsum dolor sit amet.
  7. Lorem ipsum dolor sit amet.
  8. 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:

  1. Lorem ipsum dolor sit amet.
  2. Lorem ipsum dolor sit amet.
  3. Lorem ipsum dolor sit amet.
  4. Lorem ipsum dolor sit amet.
  5. Lorem ipsum dolor sit amet.
  6. Lorem ipsum dolor sit amet.
  7. Lorem ipsum dolor sit amet.
  8. 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:

  1. Lorem ipsum dolor sit amet.
  2. Lorem ipsum dolor sit amet.
  3. Lorem ipsum dolor sit amet.
  4. Lorem ipsum dolor sit amet.
  5. Lorem ipsum dolor sit amet.
  6. Lorem ipsum dolor sit amet.
  7. Lorem ipsum dolor sit amet.
  8. 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 1.
  • Item 2.
  • Item 3.
  • Item 4.
  • Item 5.
  • 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 1.
  • Item 2.
  • Item 3.
  • Item 4.
  • Item 5.
  • Item 6.
  • Item 7.
  • Item 8.

Happy Coding!


Leave a comment

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

S$0.99
S$2.00
S$5.00

Or enter a custom amount

S$

Thank you for your contribution!

Donate

Topics:

Clean CSS (5) CSS (35) css pseudo (4) debug (2) design (7) fonts (2) html (8) image (9) JavaScript (1) layout (5) MiniGame (3) optimization (15) performance (10) responsive (9)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements