css3選取器:nth-child和nth-of-type之間的差別

來源:互聯網
上載者:User

標籤:span   bsp   section   png   blog   標籤   cti   之間   變化   

<section>

      <p>我是第1個p標籤</p>

      <p>我是第2個p標籤</p>

</section>

然後對應2個選取器對應的CSS代碼如下:

p:nth_child(2) { color:red;}  

//   p:nth-of-type(2) {  color:red;}

兩種對應的效果都是這樣的:

對於:nth-child選取器有兩點需要說明:

1,這個是個段落元素

2,這個是父級元素的第二個孩子

 

而對於:nth-of-type選取器,意味著選擇一個元素如果:

1,選擇父級元素的第二個  段落    子   元素  

 

如果把上面的代碼稍微修改一下:

<section>

    <div>我是一個普通的div標籤</div>

    <p>我是第一個p標籤</p>

    <p>我是第二個p標籤</p>

</section>

這個時候,p:nth-child(2) { color:red;} 並沒有選擇上任何的元素 ,所以並沒有變化。

p:nth-of-type(2) { color:red;} 的顯示結果是:

 

css3選取器:nth-child和nth-of-type之間的差別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.