Select last child when odd, 2 last childs when even

后端 未结 3 978
失恋的感觉
失恋的感觉 2020-12-31 14:01

I\'m in a situation where the number of elements showed is variable, and I need a strange solution which I\'m not able to achieve, I even doubt if it\'s achievable only with

3条回答
  •  無奈伤痛
    2020-12-31 14:36

    You can use CSS like so:

    li:last-child:nth-child(odd) {
        /* Last child AND odd */
        background: red;
    }
    
    li:nth-last-child(2):nth-child(odd),
    li:last-child:nth-child(even) {
        /* Before last child AND odd */
        /* Last child AND even */
        background: green;
    }
    

    Demo: https://jsfiddle.net/hw0ehrhy/

提交回复
热议问题