CSS3中nth-of-type和nth-last-of-type

1.使用nth-child和nth-last-child时会产生的问题

在使用nth-child和nth-last-child时,其计算子元素是奇数个元素还是第偶数个元素时,是连同父元素中的所有子元素一起计算的。

当父元素是列表时,列表中只可能有列表项目一种子元素,所以不会出问题,但是当父元素是div时,div包含多种子元素,会出现问题。

2.使用nth-of-type和nth-last-of-type

<style type="text/css">

h2:nth-of-type(odd){

   background:yellow;

}

h2:nth-of-type(even){

  background:blue;

}

</style>

//只计算h2元素的,而不会包含和h2同级别的其他元素

 

posted on 2016-09-03 11:30  dataman  阅读(350)  评论(0编辑  收藏  举报

导航