CSS3 的 :nth-child 定义和用法介绍

CSS3里面的 「:nth-child」是非常能提高效率的技巧,简单说不用单独给标签加上ID或Class,可以通过CSS控制页面元素的第n个元素,或倒数第n个元素,或从第m个到第n个范围子元素的样式,甚至包括奇数位和偶数位过滤。

下面介绍下 nth-child 的用法,注意低版的 IE浏览器(IE6/7) 不支持 nth-child 哦!

:nth-child(2)选取第几个标签,“2可以是你想要的数字”

.demo01 li:nth-child(2){background:#090}

:nth-child(n+4)选取大于等于4标签,“n”表示从整数,下同

.demo01 li:nth-child(n+4){background:#090}

:nth-child(-n+4)选取小于等于4标签

.demo01 li:nth-child(-n+4){background:#090}

:nth-child(2n)选取偶数标签,2n也可以是even

.demo01 li:nth-child(2n){background:#090}

:nth-child(2n-1)选取奇数标签,2n-1可以是odd

.demo01 li:nth-child(2n-1){background:#090}

:nth-child(3n+1)自定义选取标签,3n+1表示“隔二取一”

.demo01 li:nth-child(3n+1){background:#090}

:last-child选取最后一个标签

.demo01 li:last-child{background:#090}

:nth-last-child(3)选取倒数第几个标签,3表示选取第3个

.demo01 li:nth-last-child(3){background:#090}

上面就是 CSS3 的 :nth-child 介绍了,但当掌握了以后能大大提高你的代码效率,并且节省代码量,想看实例效果可以「点这里