1. 选取第一个元素,在css添加 e:first-child
div:first-child{
background-color: blue;
}
2. 选取最后一个元素 e:last-child
div:last-child{
background-color: pink;
}
3. 选取里面第几个的元素 e:nth-child(number)
div:nth-child(4){
background-color: plum;
}
4. 选取里面偶数元素 e:nth-child(2n)
div:nth-child(2n){
background-color: green;
}
5. 选取里面奇数的元素 e:nth-child(2n-1)
div:nth-child(2n-1){
background-color: #FFFFFF;
}
6. 选取从里面第三个开始到最后一个 e:nth-child(n+3)
div:nth-child(n+3){
background-color: blanchedalmond;
}
7. 选取从0到第三个的所有元素 e:nth-child(-n+3)
div:nth-child(-n+3){
background-color: indianred;
}
8. 选取倒数第三个元素 e:nth-last-child(3)
div:nth-last-child(3){
background-color: gray;
}
9. 选取从倒数第三到第一个之间的元素 e:nth-last-child(n+3)
div:nth-last-child(n+3){
background-color: gray;
}