Select the last 3 child elements

后端 未结 3 879
一个人的身影
一个人的身影 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:50

    You can read more here about nth-last child, but this should basically do the trick of selecting the last 3 children with just CSS

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

    fiddle demonstration from Fabrício Matté

    This will only select those rows returning a positive number for out N expression (-n+3), and since we are using nth-last-child, it's counting from last to first, so first rows from bottom gives,

    f(n) = -n+3
    f(1) = -1+3 = 2 <- first row from the bottom
    f(2) = -2+3 = 1 <- second row from the bottom
    f(3) = -3+3 = 0 <- third row from the bottom
    

    everything else will return a negative number

提交回复
热议问题