Discover how a single CSS rule can save you hours of tedious manual work!
Why Nth-child selector in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to color every third item in a long list on your webpage. You try to add a special class to each third item manually in your HTML.
Manually adding classes to each third item is slow and boring. If you add or remove items, you must update all the classes again, which is easy to forget and causes mistakes.
The nth-child selector in CSS lets you style every nth element automatically. You write one rule, and the browser applies it to the right items, even if the list changes.
<li class="highlight">Item 3</li> <li>Item 4</li> <li class="highlight">Item 6</li>
li:nth-child(3n) {
color: red;
}You can easily style patterns in lists or grids without changing your HTML, saving time and avoiding errors.
On a product page, you want to highlight every 4th product with a special background color to make the layout more interesting and easier to scan.
Manually styling repeated patterns is slow and error-prone.
nth-child automates styling every nth element with one CSS rule.
This keeps your HTML clean and your styles flexible.
Practice
li:nth-child(3) select?Solution
Step 1: Understand
Thenth-childselectornth-child(n)selector targets the element that is the nth child of its parent, counting all types of children.Step 2: Apply to
This means it selects theli:nth-child(3)lielement only if it is the third child of its parent. It does not select the thirdliif other elements come before it.Final Answer:
The third child of any type inside the parent -> Option CQuick Check:
li:nth-child(3)= third child of any type [OK]
- Thinking it selects the nth element of that type globally
- Confusing nth-child with nth-of-type selector
- Assuming it selects every nth element regardless of type
div inside a container using :nth-child?Solution
Step 1: Recall
The keywordevenkeyword meaningevenin:nth-child(even)selects all even-numbered children (2nd, 4th, 6th, etc.).Step 2: Match syntax to select even
Usingdivelementsdiv:nth-child(even)selects everydivthat is an even child of its parent.Final Answer:
div:nth-child(even) -> Option BQuick Check:
even = every 2nd child [OK]
- Using odd instead of even
- Using formulas like 2n+1 which select odd children
- Confusing nth-child with nth-of-type
<ul> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> <li>Five</li> </ul>
And CSS:
li:nth-child(2n) { color: red; }Which list items will appear red in the browser?
Solution
Step 1: Understand
The formula2ninnth-child2nselects every even child: 2nd, 4th, 6th, etc.Step 2: Apply to the list items
Items 2 (Two) and 4 (Four) are even children, so they get the red color.Final Answer:
Two and Four -> Option DQuick Check:
2n = even children = Two, Four [OK]
- Thinking 2n selects odd children
- Confusing nth-child with nth-of-type
- Assuming all items get styled
p element blue?p:nth-child(3n+1) {
color: blue;
}Solution
Step 1: Analyze the formula
The formula3n+13n+1selects children at positions 1, 4, 7, 10, ...Step 2: Compare with the goal of every 3rd element
Every 3rd element means positions 3, 6, 9, ... which is3n, not3n+1.Final Answer:
It colors the 1st, 4th, 7th <p>, not every 3rd -> Option AQuick Check:
3n+1 = 1,4,7... not every 3rd [OK]
- Using 3n+1 instead of 3n for every 3rd child
- Confusing formula offsets
- Expecting 3n+1 to select 3rd, 6th, 9th
li elements inside a ul without styling the 6th or others. Which CSS selector achieves this?Solution
Step 1: Understand the goal
We want to style only the 2nd and 4thlielements, excluding the 6th or any others.Step 2: Evaluate each option
li:nth-child(2n):not(:nth-child(6)) selects every evenliexcept the 6th, but also includes 8th, 10th, etc. li:nth-child(2n) selects all evenlielements (2nd, 4th, 6th, ...). li:nth-child(2n+2) selects 2nd, 4th, 6th, ... as well. li:nth-child(2), li:nth-child(4) explicitly selects only the 2nd and 4thlielements.Final Answer:
li:nth-child(2), li:nth-child(4) -> Option AQuick Check:
Explicitly list 2nd and 4th for exact selection [OK]
- Using formulas that select more than needed
- Trying to exclude with :not() but missing others
- Assuming 2n+2 excludes 6th child
