好的,所以我正在阅读 http://css-tricks.com/how-nth-child-works/ (并环顾网络)关于 :nth-child 伪类,但我找不到与我遇到的问题相关的任何信息:
有没有办法选择,例如,不管你有多少元素,都选择最后一个之前的元素?
例如:我在导航栏中有一个水平链接列表,但无论该导航栏中有多少链接,我都需要选择最后一个之前的那个。
这有意义吗?可以使用 :nth-child 伪类来完成吗?...或者以任何其他方式来完成?
我已经为这个问题绞尽脑汁好几天了。
谢谢。
您可以使用 :nth-last-child(2)
:
li:nth-last-child(2) {
color: red;
}
li:nth-last-child(2) {
color: red;
}
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
关于 :nth-last-child
:
The :nth-last-child(an+b) CSS pseudo-class matches an element that has
an+b-1 siblings after it in the document tree, for a given positive or
zero value for n, and has a parent element.
In effect, it functions the same as :nth-child except it selects items
counting backwards from the end of element series, not the start.