I have the following selectors:
.progress:nth-child(3n+1) {
background: teal;
}
.progress:nth-child(3n+2) {
background: red;
}
.progress:nth-child(3n+3) {
background: blue;
}
However all of the items end up with a teal background. Are these selectors correct? I’m thinking I should get:
- Teal (every 3, starting with 1)
- Red (every 3, starting with 2)
- Blue (every 3, starting with 3)
etc.
I’ve tested on Firefox 3.5.8 and Opera 10.10 on Ubuntu. Also tested with nothing but these rules in the CSS. I’m using the YUI Reset stylesheet but excluding it does nothing.
Your CSS seems to be correct, assuming the following HTML:
Perhaps you misunderstood the meaning of
:nth-child, and your combination of HTML/CSS is incorrect.foo:nth-childdoesn’t mean ‘every element that is the the nth child offoo’ but ‘every nthfooelement’.Pro tip: use the
:nth-child()tester. Or this one: http://leaverou.me/demos/nth.html