Bird
Raised Fist0
CSSmarkup~10 mins

Nth-child selector in CSS - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select every 3rd list item using the nth-child selector.

CSS
li[1] { color: blue; }
Drag options to blanks, or click blank then click option'
A:nth-child(odd)
B:nth-child(even)
C:nth-child(2n)
D:nth-child(3n)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2n selects every 2nd element, not every 3rd.
Using odd selects every odd element, not every 3rd.
2fill in blank
medium

Complete the code to select the 4th list item only.

CSS
li[1] { font-weight: bold; }
Drag options to blanks, or click blank then click option'
A:nth-child(3n)
B:nth-child(odd)
C:nth-child(4)
D:nth-child(even)
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd or even selects multiple elements, not just one.
Using 3n selects every 3rd element, not the 4th.
3fill in blank
hard

Fix the error in the selector to select every even list item.

CSS
li[1] { background-color: lightgray; }
Drag options to blanks, or click blank then click option'
A:nth-child(odd)
B:nth-child(even)
C:nth-child(2n+1)
D:nth-child(3n)
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd selects odd elements, not even.
Using 2n+1 selects odd elements, not even.
4fill in blank
hard

Fill BLANK_1 to select every 3rd list item starting from the 2nd (2,5,8...) with red color. Fill BLANK_2 to select every 3rd list item starting from the 1st (1,4,7...) with italic style.

CSS
li[1] { color: red; }
li[2] { font-style: italic; }
Drag options to blanks, or click blank then click option'
A:nth-child(3n+2)
B:nth-child(3n)
C:nth-child(3n+1)
D:nth-child(2n)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3n selects every 3rd starting at 3 (3,6,9), not 2.
Confusing the offset number in the formula.
5fill in blank
hard

Fill all three blanks to create a dictionary with CSS selectors as keys and colors as values: every 2nd item starting at 1st (odds: red) and every 2nd starting at 3rd (green).

CSS
const colors = {
  "[1]": "[2]",
  "[3]": "green"
};
Drag options to blanks, or click blank then click option'
Ali:nth-child(2n+1)
Bred
Cli:nth-child(2n+3)
Dblue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting that object keys must be strings (quotes are added in code).
Using color names without considering the string quotes in values.

Practice

(1/5)
1. What does the CSS selector li:nth-child(3) select?
easy
A. The third <li> element inside its parent
B. Every third <li> element in the whole document
C. The third child of any type inside the parent
D. All <li> elements except the third one

Solution

  1. Step 1: Understand nth-child selector

    The nth-child(n) selector targets the element that is the nth child of its parent, counting all types of children.
  2. Step 2: Apply to li:nth-child(3)

    This means it selects the li element only if it is the third child of its parent. It does not select the third li if other elements come before it.
  3. Final Answer:

    The third child of any type inside the parent -> Option C
  4. Quick Check:

    li:nth-child(3) = third child of any type [OK]
Hint: Selects element if it is nth child of parent, not nth of type [OK]
Common Mistakes:
  • 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
2. Which of the following is the correct syntax to select every even div inside a container using :nth-child?
easy
A. div:nth-child(2n+1)
B. div:nth-child(even)
C. div:nth-child(odd)
D. div:nth-child(2n-1)

Solution

  1. Step 1: Recall even keyword meaning

    The keyword even in :nth-child(even) selects all even-numbered children (2nd, 4th, 6th, etc.).
  2. Step 2: Match syntax to select even div elements

    Using div:nth-child(even) selects every div that is an even child of its parent.
  3. Final Answer:

    div:nth-child(even) -> Option B
  4. Quick Check:

    even = every 2nd child [OK]
Hint: Use 'even' keyword to select every 2nd child easily [OK]
Common Mistakes:
  • Using odd instead of even
  • Using formulas like 2n+1 which select odd children
  • Confusing nth-child with nth-of-type
3. Given this HTML:
<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?
medium
A. Only Two
B. One, Three, and Five
C. All list items
D. Two and Four

Solution

  1. Step 1: Understand 2n in nth-child

    The formula 2n selects every even child: 2nd, 4th, 6th, etc.
  2. Step 2: Apply to the list items

    Items 2 (Two) and 4 (Four) are even children, so they get the red color.
  3. Final Answer:

    Two and Four -> Option D
  4. Quick Check:

    2n = even children = Two, Four [OK]
Hint: 2n selects even children: 2,4,6... [OK]
Common Mistakes:
  • Thinking 2n selects odd children
  • Confusing nth-child with nth-of-type
  • Assuming all items get styled
4. What is wrong with this CSS if the goal is to color every 3rd p element blue?
p:nth-child(3n+1) {
  color: blue;
}
medium
A. It colors the 1st, 4th, 7th p, not every 3rd
B. Syntax error in the formula
C. It colors only the 3rd p element
D. It colors all p elements

Solution

  1. Step 1: Analyze the formula 3n+1

    The formula 3n+1 selects children at positions 1, 4, 7, 10, ...
  2. Step 2: Compare with the goal of every 3rd element

    Every 3rd element means positions 3, 6, 9, ... which is 3n, not 3n+1.
  3. Final Answer:

    It colors the 1st, 4th, 7th <p>, not every 3rd -> Option A
  4. Quick Check:

    3n+1 = 1,4,7... not every 3rd [OK]
Hint: Use 3n for every 3rd, not 3n+1 [OK]
Common Mistakes:
  • Using 3n+1 instead of 3n for every 3rd child
  • Confusing formula offsets
  • Expecting 3n+1 to select 3rd, 6th, 9th
5. You want to style only the 2nd and 4th li elements inside a ul without styling the 6th or others. Which CSS selector achieves this?
hard
A. li:nth-child(2), li:nth-child(4)
B. li:nth-child(2n)
C. li:nth-child(2n+2)
D. li:nth-child(2n):not(:nth-child(6))

Solution

  1. Step 1: Understand the goal

    We want to style only the 2nd and 4th li elements, excluding the 6th or any others.
  2. Step 2: Evaluate each option

    li:nth-child(2n):not(:nth-child(6)) selects every even li except the 6th, but also includes 8th, 10th, etc. li:nth-child(2n) selects all even li elements (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 4th li elements.
  3. Final Answer:

    li:nth-child(2), li:nth-child(4) -> Option A
  4. Quick Check:

    Explicitly list 2nd and 4th for exact selection [OK]
Hint: List exact children with commas for precise selection [OK]
Common Mistakes:
  • Using formulas that select more than needed
  • Trying to exclude with :not() but missing others
  • Assuming 2n+2 excludes 6th child