How can I style even and odd elements?

后端 未结 9 2054
离开以前
离开以前 2020-11-22 10:44

Is it possible to use CSS pseudo-classes to select even and odd instances of list items?

I\'d expect the following to produce a list of alternating colors, but inste

9条回答
  •  名媛妹妹
    2020-11-22 11:13

    the css odd and even is not support for IE. recommend you using solution below.

    Best solution:

    li:nth-child(2n+1) { color:green; } // for odd
    li:nth-child(2n+2) { color:red; } // for even
    
    li:nth-child(1n) { color:green; }
    li:nth-child(2n) { color:red; }
    
    • list element 1
    • list element 2
    • list element 3
    • list element 4

提交回复
热议问题