CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

 

CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

  虽然说目前CSS3还没正式的标准化,但新的属性已为网页带来许多的便利,像是大家所熟悉的圆角框、阴影、渐层、多栏位….,甚至目前正夯的RWD自适应,这些都是需要使用到CSS3,除此之外CSS3还提供了许多的选择器可使用,如此一来就可大量的减少一些类别的设定,直接透过HTML的标签就可直接进行选择与样式的设定,虽然说很方便,但在CSS3有二个选择器长的很像nth-child与nth-of-type,在正常情况下,二个用起来很像,但若HTML的结构改变时,二个就大不同啦!!!因此梅干作了一个小范例,来帮大家解惑,这二个属性的最大差别什么地方。

 

:nth-of-type() 这个 CSS 伪类匹配文档树中在其之前具有 an+b-1 个相同兄弟节点的元素,其中 n 为正值或零值。简单点说就是,这个选择器匹配那些在相同兄弟节点中的位置与模式 an+b 匹配的相同元素。
/* 在每组兄弟元素中选择第四个 <p> 元素 */
p:nth-of-type(4n) {
  color: lime;
}

 

语法

nth-of-type伪类指定一个实际参数,这个参数使用一种模式来匹配哪些元素应该被选中。

 

正式语法

:nth-of-type( <nth> )

where
<nth> = <an-plus-b> | even | odd

 

示例

Basic example

HTML

<div>
  <div>这段不参与计数。</div>
  <p>这是第一段。</p>
  <p>这是第二段。</p>
  <div>这段不参与计数。</div>
  <p>这是第三段。</p>
  <p>这是第四段。</p>
</div>

CSS

/* 奇数段 */
p:nth-of-type(2n+1) {
  color: red;
}

/* 偶数段 */
p:nth-of-type(2n) {
  color: blue;
}

/* 第一段 */
p:nth-of-type(1) {
  font-weight: bold;
}

 

:nth-child(an+b) 这个 CSS 伪类首先找到所有当前元素的兄弟元素,然后按照位置先后顺序从1开始排序,选择的结果为第(an+b)个元素的集合(n=0,1,2,3…)。示例:

  • 0n+3 或简单的 3 匹配第三个元素。
  • 1n+0 或简单的 n 匹配每个元素。(兼容性提醒:在 Android 浏览器 4.3 以下的版本 n和 1n 的匹配方式不一致。1n 和 1n+0 是一致的,可根据喜好任选其一来使用。)
  • 2n+0 或简单的 2n 匹配位置为 2、4、6、8…的元素(n=0时,2n+0=0,第0个元素不存在,因为是从1开始排序)。你可以使用关键字 even 来替换此表达式。
  • 2n+1 匹配位置为 1、3、5、7…的元素。你可以使用关键字 odd 来替换此表达式。
  • 3n+4 匹配位置为 4、7、10、13…的元素。

a 和 b 都必须为整数,并且元素的第一个子元素的下标为 1。换言之就是,该伪类匹配所有下标在集合 { an + b; n = 0, 1, 2, …} 中的子元素。另外需要特别注意的是,an 必须写在 b 的前面,不能写成 b+an 的形式。

 

示例

选择器示例

tr:nth-child(2n+1)
表示HTML表格中的奇数行。
tr:nth-child(odd)
表示HTML表格中的奇数行。
tr:nth-child(2n)
表示HTML表格中的偶数行。
tr:nth-child(even)
表示HTML表格中的偶数行。
span:nth-child(0n+1)
表示子元素中第一个且为span的元素,与 :first-child 选择器作用相同。
span:nth-child(1)
表示父元素中子元素为第一的并且名字为span的标签被选中
span:nth-child(-n+3)
匹配前三个子元素中的span元素。

 

HTML

<h3><code>span:nth-child(2n+1)</code>, WITHOUT an
   <code>&lt;em&gt;</code> among the child elements.</h3>
<p>Children 1, 3, 5, and 7 are selected.</p>
<div class="first">
  <span>Span 1!</span>
  <span>Span 2</span>
  <span>Span 3!</span>
  <span>Span 4</span>
  <span>Span 5!</span>
  <span>Span 6</span>
  <span>Span 7!</span>
</div>

<br>

<h3><code>span:nth-child(2n+1)</code>, WITH an
   <code>&lt;em&gt;</code> among the child elements.</h3>
<p>Children 1, 5, and 7 are selected.<br>
   3 is used in the counting because it is a child, but it isn't
   selected because it isn't a <code>&lt;span&gt;</code>.</p>
<div class="second">
  <span>Span!</span>
  <span>Span</span>
  <em>This is an `em`.</em>
  <span>Span</span>
  <span>Span!</span>
  <span>Span</span>
  <span>Span!</span>
  <span>Span</span>
</div>

<br>

<h3><code>span:nth-of-type(2n+1)</code>, WITH an
   <code>&lt;em&gt;</code> among the child elements.</h3>
<p>Children 1, 4, 6, and 8 are selected.<br>
   3 isn't used in the counting or selected because it is an <code>&lt;em&gt;</code>, 
   not a <code>&lt;span&gt;</code>, and <code>nth-of-type</code> only selects
   children of that type. The <code>&lt;em&gt;</code> is completely skipped
   over and ignored.</p>
<div class="third">
  <span>Span!</span>
  <span>Span</span>
  <em>This is an `em`.</em>
  <span>Span!</span>
  <span>Span</span>
  <span>Span!</span>
  <span>Span</span>
  <span>Span!</span>
</div>

CSS

html {
  font-family: sans-serif;
}

span,
div em {
  padding: 5px;
  border: 1px solid green;
  display: inline-block;
  margin-bottom: 3px;
}

.first span:nth-child(2n+1),
.second span:nth-child(2n+1),
.third span:nth-of-type(2n+1) {
  background-color: lime;
}

 

 

 

CSS3-:nth-child()
当网页中有4个p标签时,可用 nth-child的方式,来进行选择,当下odd表示,只要是单数列背景就会套用成浅灰色。
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类


这时候只要是奇数列的p标签,背景就会变成浅灰色,在这种情况下,即便使用nth-of-type也会得到一样的效果。
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类


改变一下网页的架构,把p标签下方,加入<hr>来将区块与区块间来画一条。
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类


 

这时就会看到,全有p标签的背景都变成浅灰色的了。

CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

 

原因就出在,当使用nth-child时,它并不是只筛选p标签,而是网页中的所有标签都会一起计算,所以可看到,这时所有的p就变成奇数列了。

CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

CSS3-:nth-of-type()
这时只要把原本的nth-child改成nth-of-type,就只会针对网页中的p标签进行筛选。
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类


锵~锵~这时候就只有在奇数列的p标签的背景才会套用浅灰色
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法,  CSS 伪类
CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

本文: CSS3选择器「:nth-child()」与「:nth-of-type()」用法大不同, :nth-of-type 用法, CSS 伪类

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.