【发布时间】:2011-06-18 04:25:33
【问题描述】:
使用标准列表,我试图选择最后 2 个列表项。我有 An+B 的各种排列,但似乎没有选择最后 2 个:
li:nth-child(n+2) {} /* selects from the second onwards */
li:nth-child(n-2) {} /* selects everything */
li:nth-child(-n+2) {} /* selects first two only */
li:nth-child(-n-2) {} /* selects nothing */
我知道有一个新的 CSS3 选择器,例如 :nth-last-child(),但如果可能的话,我更喜欢在更多浏览器中工作的东西(不要特别关心 IE)。
【问题讨论】:
-
尽管有 IE,但浏览器对
:nth-last-child()的支持与:nth-child()according to quirksmode.org 大致相同。此外,:nth-child()和:nth-last-child()都是在 CSS3 中引入的,在这个意义上既不是旧的也不是新的。 -
nth-child总结了很多有用的技巧css-tricks
标签: css css-selectors