CSS3选择器:nth-child与:nth-of-type区别
2017-06-24 19:17
197 查看
一、:nth-child
1.1 说明
:nth-child(n)选择器匹配属于其父元素的第N个子元素,不论元素的类型。n可以是数字、关键词或公式。注意:如果第N个子元素与选择的元素类型不同则样式无效!
1.2 示例
<style> div>p:nth-child(2){ color:red; } </style> <div> <p>我是第1个段落</p> <p>我是第2个段落</p><!--符合条件:1、是<p>元素,2、父元素<div>的第二个元素。这里被选择,会变成红色。--> <p>我是第3个段落</p> </div> <div> <p>我是第1个段落</p> <span>我是第1个文本</span><!--不符合条件:不是<p>元素,没有被选择--> <p>我是第2个段落</p> </div>
1.3 改进
如果想让上面第二个<span>生效的可以去除子元素
<p>的选择,写一个父元素
<div>加一个空格,以防止
:nth-child不生效。
<style> div :nth-child(2){ //div+空格,只根据父元素选择 color:red; } </style> <div> <p>我是第1个段落</p> <span>我是第1个文本</span><!--符合条件--> <p>我是第2个段落</p> </div>
二、:nth-of-type
2.1 说明
:nth-of-type(n)选择器匹配属于父元素的特定类型的第N个子元素的每个元素。n可以是数字、关键词或公式。2.2 示例
<style> div>p:nth-of-type(2){ color:red; } </style> <div> <p>我是第1个段落</p> <p>我是第2个段落</p><!--符合条件:1、是特定元素类型<p>,2、是父元素<div>的第二个<p>元素。这里被选择,会变成红色--> <p>我是第3个段落</p> </div> <div> <p>我是第1个段落</p> <blockquote>第1个引用</blockquote> <p>我是第2个段落</p><!--符合条件:1、是特定元素类型<p>,2、是父元素<div>的第二个<p>元素。这里被选择,会变成红色--> <p>我是第3个段落</p> </div>CSS3选择器:nth-child与:nth-of-type区别
相关文章推荐
- [css3]CSS3选择器:nth-child和:nth-of-type之间的差异
- 第二章 CSS3新增的选器之 选择器 last-child last-of-type区别
- CSS3::nth-child与:nth-of-type区别
- CSS3选择器:nth-child和:nth-of-type之间的差异
- CSS3选择器:nth-child和:nth-of-type之间的差异
- css3中nth-child选择器和nth-of-type选择器的区别
- CSS3选择器:nth-child和:nth-of-type之间的差异
- CSS3选择器:nth-child和:nth-of-type之间的差异(转)
- CSS3的nth-child() 和nth-of-type()选择器,表格奇偶行变色
- CSS3选择器:nth-child和:nth-of-type之间的差异
- css3 - 选择器first-child、last-child、nth-child、nth-last-child、nth-of-type
- CSS3选择器之nth-of-type
- CSS3选择器:nth-of-type
- CSS3伪类选择器nth-child和nth-of-type浅析
- 【CSS3】---first-of-type选择器+nth-of-type(n)选择器
- CSS3 :nth-child()伪类选择器实现奇偶行显示不同样式
- CSS3:nth-child()伪类选择器…
- CSS3:nth-child(x) 选择器
- jQuery实现隔行变色、悬停变色 ( CSS3伪类选择器:nth-child() )