到目前为止,最简单的方法是使用自己的一组伪类来定位要设置样式的2个元素.
正如你所说,最后一个孩子由3n 8(8,11,14 ……)代表.然后,倒数第二个孩子是3n 7(7,10,13 ……).
li:nth-last-child(1):nth-child(3n+8),
li:nth-last-child(2):nth-child(3n+7) {
color: red;
}
ul { counter-reset: item; list-style-type: none; }
li { display: inline; padding: 0.25em; }
li::before { content: counter(item); counter-increment: item; }
您也可以使用一个复杂的选择器,使用Can CSS detect the number of children an element has?中的技术,用3n 8代替:nth-last-child()参数,并使用额外的:nth-last-child(-n 2)来定位最后两个具有一个伪类的元素:
li:first-child:nth-last-child(3n+8) ~ li:nth-last-child(-n+2) {
color: red;
}
ul { counter-reset: item; list-style-type: none; }
li { display: inline; padding: 0.25em; }
li::before { content: counter(item); counter-increment: item; }