CSS3中first-child、last-child、nth-child、nth-last-child

1.单独指定第一个子元素、最后一个子元素的样式

<style type="text/css">

li:first-child{

  background:yellow;

}

li:last-child{

   background:blue;

}

</style>

2.对指定序号的子元素使用样式

<style type="text/css">

li:nty-child(2){

  background:yellow;

}//对第二个子元素设定背景色

li:nth-last-child(2){

  background:blue;

}//对倒数第二个子元素设定背景色

</style>

3.对所有第偶数个子元素或者第奇数个子元素使用样式

<style type="text/css">

li:nth-child(odd){

  background:yellow;

}

li:nth-child(even){

 background:blue

}

</style>

原文地址:https://www.cnblogs.com/wyaocn/p/5836555.html