CSS global :nth-of-type selector (:nth-of-class)

后端 未结 2 1866
梦毁少年i
梦毁少年i 2021-01-16 05:36

What CSS selector can be used to select all odd elements inside a parent, but which are not necessarily siblings?

相关标签:
2条回答
  • 2021-01-16 05:56

    I couldn't get it down to single selector, but with two...

    .parent > .element:nth-of-type(2n){
      background:#afa !important;
    }
    .parent > div:nth-of-type(2n) > .element {
      background:#afa !important;
    }
    

    View it on JSFiddle

    0 讨论(0)
  • 2021-01-16 05:58

    While it uses JavaScript, you could style the odd element without altering your markup:

    $('.parent .element:odd').css('font-weight', 'bold');
    

    ​ http://jsfiddle.net/ansonhoyt/MfxYF/

    0 讨论(0)
提交回复
热议问题