要选择最后两个元素,可以使用 :nth-last-child(-n+x)
选择器。其中 n
表示元素总数,x
表示最后几个元素的个数。
要选择奇数个元素,可以使用 :nth-child(odd)
选择器。
要选择偶数个元素,可以使用 :nth-child(even)
选择器。
综合起来,以下三个选择器可以分别选择最后两个元素、奇数个元素和偶数个元素:
/* 选择最后两个元素 */
:nth-last-child(-n+2) {
/* 样式 */
}
/* 选择奇数个元素 */
:nth-child(odd) {
/* 样式 */
}
/* 选择偶数个元素 */
:nth-child(even) {
/* 样式 */
}