Selecting half the elements with :nth-child?

前端 未结 3 1432
迷失自我
迷失自我 2020-12-06 05:36

Take the following code for instance:

  • Hello World
  • Hello World
  • Hello World
  • &
3条回答
  •  忘掉有多难
    2020-12-06 05:58

    The only way you'd be able to get anywhere near to that in pure CSS is to do a selector on either nth-child(odd) or nth-child(even). If you want exactly the last half (and not either odd or even), then you'd have to use JavaScript/jQuery.

    Using jQuery, you could get them using:

    var yourList = $("ul li");
    
    yourList = yourList.slice(0, Math.floor(yourList.length/2));
    

提交回复
热议问题