0
0
CSSmarkup~20 mins

Nth-child selector in CSS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nth-child Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
What elements are selected by li:nth-child(3n)?
Given a list of <li> items, which items will be styled by the CSS selector li:nth-child(3n)?
CSS
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
</ul>
AItems 2, 4, and 6
BItems 1, 3, and 5
CItems 3 and 6 only
DAll items
Attempts:
2 left
💡 Hint
Remember that 3n means every 3rd element starting from 3.
🧠 Conceptual
intermediate
2:00remaining
What does :nth-child(odd) select?
Which elements are targeted by the CSS selector :nth-child(odd)?
CSS
<div>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
  <p>Paragraph 4</p>
</div>
AOnly the last paragraph
BAll paragraphs with even positions: 2, 4, 6, ...
COnly the first paragraph
DAll paragraphs with odd positions: 1, 3, 5, ...
Attempts:
2 left
💡 Hint
Odd means 1, 3, 5, and so on.
📝 Syntax
advanced
2:00remaining
What error occurs with li:nth-child(2n+)?
Consider the CSS selector li:nth-child(2n+). What happens when you use this selector in your CSS?
CSS
li:nth-child(2n+) { color: red; }
ASyntax error: incomplete expression after '+'
BSelects every 2nd element starting from 1
CSelects no elements
DSelects every element
Attempts:
2 left
💡 Hint
Check if the expression after '+' is complete.
rendering
advanced
2:00remaining
Which CSS rule colors only the 4th item in a list?
You want to color only the 4th <li> item red. Which CSS selector and rule achieves this?
CSS
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>
Ali:nth-child(4n) { color: red; }
Bli:nth-child(4) { color: red; }
Cli:nth-child(odd) { color: red; }
Dli:nth-child(3n) { color: red; }
Attempts:
2 left
💡 Hint
Use a selector that targets exactly the 4th child.
accessibility
expert
3:00remaining
How does :nth-child affect keyboard navigation and accessibility?
If you use :nth-child selectors to hide or style elements, what should you consider to keep your page accessible?
AEnsure hidden elements are removed from keyboard focus and screen readers
BNo special consideration is needed; styling does not affect accessibility
CUse <code>:nth-child</code> only on non-interactive elements
DAvoid using <code>:nth-child</code> selectors altogether
Attempts:
2 left
💡 Hint
Think about how hidden or styled elements affect users who navigate with keyboard or screen readers.