CSS3 :nth-of-type() 選取器

來源:互聯網
上載者:User

標籤:匹配   -o   code   tle   title   blog   round   特定   logs   

定義和用法

:nth-of-type(n) 選取器匹配屬於父元素的特定類型的第 N 個子項目的每個元素.

n 可以是數字、關鍵詞或公式。

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6 <style type="text/css"> 7 div p:nth-of-type(3){ 8     color:#fff; 9     background:#333;10 }11 </style>12 </head>13 <body>14     <div>15         <p>第一行</p>16         <p>第二行</p>17         <p>第三行</p>18         <p>第四行</p>19         <p>第五行</p>20     </div>21 </body>22 </html>

1.Odd 和 even 是可用於匹配下標是奇數或偶數的子項目的關鍵詞(第一個子項目的下標是 1)

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6 <style type="text/css"> 7 div p:nth-of-type(odd){ 8     color:#fff; 9     background:#333;10 }11 div p:nth-of-type(even){12     color:#fff;13     background:red;14 }15 </style>16 </head>17 <body>18     <div>19         <p>第一行</p>20         <p>第二行</p>21         <p>第三行</p>22         <p>第四行</p>23         <p>第五行</p>24     </div>25 </body>26 </html>

2.使用公式 (an + b)。描述:表示周期的長度,n 是計數器(從 0 開始),b 是位移值

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6 <style type="text/css"> 7 div p:nth-of-type(3n+0){ 8     color:#fff; 9     background:#333;10 }11 </style>12 </head>13 <body>14     <div>15         <p>第一行</p>16         <p>第二行</p>17         <p>第三行</p>18         <p>第四行</p>19         <p>第五行</p>20         <p>第六行</p>21     </div>22 </body>23 </html>

 

CSS3 :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.