How do I choose the last 2 items in a list with css nth-child?

后端 未结 4 552
不知归路
不知归路 2021-01-31 08:29

Is it possible? If not, is there a way to do it with jQuery?

4条回答
  •  盖世英雄少女心
    2021-01-31 09:21

    It is possible with CSS3. Consider the markup:

    
    

    To choose the last two LI elements you do:

    ul > li:nth-last-of-type(-n+2) {
        background: green;
    }
    

    Works in all contemporary browsers including IE9, but excluding IE8 and below. To add support for those browsers, you might consider using Selectivizr.js

提交回复
热议问题