问题描述
我知道你可以用:last-child
选择最后一个寒意。现在我需要选择最后3个孩子唯一的问题是孩子的数量是不知道所以我不能使用:nth-child
,我知道有一些东西叫做:nth-last-child
但我真的不能理解这是如何工作的
I know you can select the last chill with :last-child
. now i need to select the last 3 childs the only problem is the amount of childs is not know so i cant use :nth-child
, i know there is something that's called :nth-last-child
but i cant really understand how this is working
<div id="something">
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
<!-- here some more
and than i need to select these -->
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
</div>
所以现在我需要这样的东西:
so now i need some thing like this:
#something a:last-child{
/* only now it needs to select 2 more a's that are before this child */
}
推荐答案
您可以阅读更多关于第n个最后一个孩子,但这应该基本上选择最后3个孩子只有CSS
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*/
}
这只会选择返回正数的行表达式(-n + 3),因为我们使用nth-last-child,所以从最后到第一个数字,
,所以从底部开始的第一行给出,
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
其他一切将返回负数
这篇关于选择最后3个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!