CSS3中:nth-child和:nth-of-type的区别深入理解

关于:nth-child和:nth-of-type的区别之前一直没太注意。最近打算深入了解一些CSS3,才发现里面其实暗藏玄机。

:nth-child可以选择父元素下的字元素,:nth-of-type也可以。但是它们到底有什么区别呢?

其实区别很简单::nth-of-type为什么要叫:nth-of-type?因为它是以”type”来区分的。也就是说:ele:nth-of-type(n)是指父元素下第n个ele元素,

而ele:nth-child(n)是指父元素下第n个元素且这个元素为ele,若不是,则选择失败。

<div> 
<ul class="demo"> 
<p>zerop> 
<li>oneli> 
<li>twoli> 
ul> 
div> 

上面这个例子,.demo li:nth-child(2)选择的是

  • one
  • 节点,而.demo li:nth-of-type(2)则选择的是
  • two
  • 节点。

    你可能感兴趣的:(css)