有哪些 nth-child
?
:first-child
:first-of-type
:last-of-type
:only-of-type
:only-child
:nth-child(n)
:nth-last-child(n)
:nth-of-type(n)
:nth-last-of-type(n)
:last-child
选择第N个LI
ul.list li:nth-child(3){background: #000;}
选择前三个
ul.list li:nth-child(-n+3){background: #000;}
选择第四个到第八个
ul.list li:nth-child(n+4):nth-child(-n+8){background: #000;}
选择奇数行
ul.list li:nth-child(2n+1){background: #000;}
这里,nth-child
提供了一种简写的方法
ul.list li:nth-child(odd){background: #000;}
选择偶数行
ul.list li:nth-child(2n){background: #000;}
这里,nth-child
也提供了一种简写的方法
ul.list li:nth-child(even){background: #000;}
选择2\5\8等三倍数行
ul.list li:nth-child(3n+2){background: #000;}
选择1\4\7\10等三倍数行
ul.list li:nth-child(3n+1){background: #000;}
选择 5\10 等五倍数行
ul.list li:nth-child(5n){background: #000;}
选择第三个到第九个之间的奇数行(不包括3\9)
ul.list li:nth-child(2n+1):nth-child(n+4):nth-child(-n+8){background: #000;}
选择第三个到第九个之间的奇数行(包括3\9)
ul.list li:nth-child(2n+1):nth-child(n+3):nth-child(-n+9){background: #000;}
选择3位倍数+1的(1/4/7/10),其中的偶数
ul.list li:nth-child(3n+1):nth-child(2n){background: #000;}
如何选择最后两个
nth-last-child
选择器的用法,和 nth-child
选择器的用法是完全一致的,只有一个不同,那就是 nth-child
是从第一个开始计数的,而nth-last-child
是从倒数第一个开始计数的
ul.list li:nth-last-child(-n+2){background: #000;}
如何实现反选?
什么是反选,举例,我要选择除了1\4\7\10等三为倍数的数字之外的其他选项,如下表所示:
ul.list li:not(:nth-child(3n+1)){background: #000;}
相关文章
通过CSS截取文字长度2023-01-02
display:inline-block 水平居中无效的解决办法2019-03-07
CSS中font-family:中文字体的英文名称 2018-11-06
CSS3 transform多种组合2018-10-26
scrollbar CSS美化(仿Safari样式)2013-10-28
CSS样式在IE6中失效的解决方案2013-05-06
chrome margin失效的解决办法2013-04-19
使用css调整文字间距2013-04-10
div 垂直居中完美解决办法2013-03-18
background 透明,文字不透明2012-06-03