How can I style even and odd elements?
Demo: http://jsfiddle.net/thirtydot/K3TuN/1323/
li {
color: black;
}
li:nth-child(odd) {
color: #777;
}
li:nth-child(even) {
color: blue;
}
<ul>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
</ul>
Documentation:
- https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
- http://caniuse.com/css-sel3 (it works almost everywhere)
Use this:
li { color:blue; }
li:nth-child(odd) { color:green; }
li:nth-child(even) { color:red; }
See here for info on browser support: http://kimblim.dk/css-tests/selectors/
The problem with your CSS lies with the syntax of your pseudo-classes.
The even and odd pseudo-classes should be:
li:nth-child(even) {
color:green;
}
and
li:nth-child(odd) {
color:red;
}
Demo: http://jsfiddle.net/q76qS/5/
But it's not working in IE.
Recommend using
:nth-child(2n+1)
:nth-child(2n+2)
li {
color: black;
}
li:nth-child(odd) {
color: #777;
}
li:nth-child(even) {
color: blue;
}
<ul>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
</ul>