CSS选择器,用于选择最后两个子项,而不知道列表中有多少项 [英] CSS selector for selecting the last two children, without knowing how many items are in the list

查看:3820
本文介绍了CSS选择器,用于选择最后两个子项,而不知道列表中有多少项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无序的列表。它有时包含4,5,6或7项。我想知道是否有一个CSS选择器来选择最后两个项目。我意识到:last-child 会给我最后一个项目。是否有秒到最后一个孩子选择器?或number of children -2选择器?

I have an unordered list. It sometimes contains 4, 5, 6, or 7 items. I would like to know if there is a CSS selector to select the last two items. I realize that :last-child will get me the last item. Is there a "second to last child" selector? or "number of children -2" selector?

HTML:

<ul>
  <li>First Item</li>
  <li>Second Item</li>
  <li>Second from Last Item</li>
  <li>Last Item</li>
<ul>

CSS:

ul li:last-child, ul li:last-child-1 {
  // Do something
}

我意识到:last-child-1 不是一个选择器,但希望有类似的东西。

I realize :last-child-1 is not a selector but im hoping there is something similar out there. Any help would be greatly apprecialted.

推荐答案

ul li:nth-last-child(-n + 2) {
    /* .. */
}

这篇关于CSS选择器,用于选择最后两个子项,而不知道列表中有多少项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆