Styling Every Third List Item Using :nth-child() in CSS
You can use the :nth-child() pseudo-class in CSS to style elements based on their order within a parent. To target every third list item, you can use the pattern 3n inside :nth-child().
:nth-child() selects elements based on their position within a parent element.
You can use formulas like 2n, 3n, or 3n+1 to create repeating patterns.
li:nth-child(3n) targets every third list item in a list.
In this example, the :nth-child(3n) selector highlights every third list item with a blue background and bold text. You can adjust the number to style elements in other repeating sequences.
Use :nth-child() for pattern-based styling instead of adding extra classes.
Remember that counting starts at 1, not 0.
Use :nth-of-type() when you only want to target specific element types among mixed children.
Combine with transitions for smooth visual effects.
How would you style every third list item in an unordered list to have a different background color?
What happens if you use :nth-child(3) instead of :nth-child(3n)? Why doesn't that work for every third item?
Our product page shows a list of featured items, but after adding a promotional banner between items, every third item stopped getting the highlight style — what’s likely broken and how would you fix it?
A designer wants every third list item to have a border, but the list is rendered conditionally — sometimes it’s empty or has only 2 items. How do you ensure the styling is robust and doesn’t cause layout shifts?
We’re building a scalable component library where lists can be nested arbitrarily — how would you design a CSS rule to style every third item without affecting nested lists or breaking with dynamic content injection?
A performance audit showed CSS reflows on list updates. How would you optimize the styling of every third item to minimize layout thrashing in a high-frequency update scenario?
We’re migrating a legacy app with hundreds of hardcoded list styles to a CSS-in-JS system — how do you ensure the 'every third item' pattern is preserved consistently across teams without introducing specificity wars or runtime performance penalties?
Our design system supports internationalization and dynamic content lengths — how would you architect the CSS strategy for this pattern to remain maintainable across 50+ product teams with different styling conventions and deployment cycles?