CSS3 :nth-child() Selector: Quick Visual Guide
The :nth-child() pseudo-class lets you target elements based on their position among siblings.
Here's what each pattern selects (yellow = matched):
πΉfirst-child β 1st child only
πΉlast-child β Last child only
πΉnth-child(2) β Exactly the 2nd child
πΉnth-last-child(2) β 2nd from the end
πΉnth-child(odd) β 1, 3, 5, 7β¦ (odd positions)
πΉnth-child(even) β 2, 4, 6, 8β¦ (even positions)
πΉnth-child(n+4) β From 4th onward (4, 5, 6β¦)
πΉnth-child(3n+1) β 1, 4, 7, 10β¦ (every 3rd, starting at 1)
πΉnth-child(3n-1) β 2, 5, 8, 11β¦ (every 3rd, offset by -1)
πΉnth-child(4n) β 4, 8, 12β¦ (every 4th child)
β Pro tip:n starts at 0. so 3n+1 hits 1st, 4th, 7thβ¦
The :nth-child() pseudo-class lets you target elements based on their position among siblings.
Here's what each pattern selects (yellow = matched):
πΉfirst-child β 1st child only
πΉlast-child β Last child only
πΉnth-child(2) β Exactly the 2nd child
πΉnth-last-child(2) β 2nd from the end
πΉnth-child(odd) β 1, 3, 5, 7β¦ (odd positions)
πΉnth-child(even) β 2, 4, 6, 8β¦ (even positions)
πΉnth-child(n+4) β From 4th onward (4, 5, 6β¦)
πΉnth-child(3n+1) β 1, 4, 7, 10β¦ (every 3rd, starting at 1)
πΉnth-child(3n-1) β 2, 5, 8, 11β¦ (every 3rd, offset by -1)
πΉnth-child(4n) β 4, 8, 12β¦ (every 4th child)
β Pro tip:
β€3