HTML/CSSIntermediate#css#selectors

Explain the :nth-child() selector with examples.

nth-child(n) selects elements based on their position among siblings, accepting keywords (odd/even) or formulas (an+b). It counts ALL sibling elements matching the selector regardless of type, unlike nth-of-type which only counts same-type siblings.

Example
li:nth-child(2n) { background: #eee; }   /* even rows */
li:nth-child(3n+1) { color: blue; }      /* 1st, 4th, 7th... */
p:nth-of-type(1) { font-weight: bold; }  /* first <p> among siblings */

Related Questions

1
HTML/CSSIntermediate#css#selectors

What CSS attribute selectors are available?

Open
2
HTML/CSSIntermediate#css#selectors

What is the difference between adjacent sibling (+) and general sibling (~) combinators?

Open
3
HTML/CSSBeginner#css#transitions

How do CSS transitions work?

Open