Select the last 3 child elements

后端 未结 3 881
一个人的身影
一个人的身影 2021-02-01 12:15

I know you can select the last child with :last-child or a certain child with :nth-child (if you know their position/number).

What I need is to

3条回答
  •  渐次进展
    2021-02-01 12:57

    The accepted answer has the correct formula, but the explanation is wrong.

    So the correct CSS is (same as currently accepted answer):

    #something a:nth-last-child(-n+3) {
        /*declarations*/
    }
    

    But here is the correct explanation of the math:

    f(n) = -n+3
    f(0) = -0+3 = 3 <- 3rd row from the bottom
    f(1) = -1+3 = 2 <- 2nd row from the bottom
    f(2) = -2+3 = 1 <- 1st row from the bottom
    f(3) = -3+3 = 0 <- nothing
    f(3) = -4+3 = -1 <- nothing
    etc...
    

提交回复
热议问题