nth-child with mod (or modulo) operator

后端 未结 5 1699
忘了有多久
忘了有多久 2020-12-16 13:10

Is it possible to use the nth-child with modulo? I know you can specify a formula, such as

nth-child(4n+2)

But I can\'t seem t

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 13:24

    No, :nth-child() only supports addition, subtraction and coefficient multiplication.

    I gather you're trying to pick up the first 6 elements (as n mod 7 for any positive integer n only gives you 0 to 6). For that, you can use this formula instead:

    :nth-child(-n+6)
    

    By negating n, element counting is done backwards starting from zero, so these elements will be selected:

     0 + 6 = 6
    -1 + 6 = 5
    -2 + 6 = 4
    -3 + 6 = 3
    -4 + 6 = 2
    -5 + 6 = 1
    ...
    

    jsFiddle demo

提交回复
热议问题