為每個作為其父

元素的第 n 個

元素設定樣式


使用 CSS :nth-of-type(n) 選擇器為每個作為其父元素的第 n 個

元素設定樣式。你可以嘗試執行以下程式碼來實現 :nth-of-type(n) 選擇器

示例

即時演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:nth-of-type(2) {
            background: yellow;
         }
      </style>
   </head>
   <body>
      <p>This is demo text 1.</p>
      <p>This is demo text 2.</p>
      <p>This is demo text 3.</p>
      <p>This is demo text 4.</p>
   </body>
</html>

更新於: 2020 年 6 月 30 日

127 次瀏覽

開啟您的 職業生涯

完成該課程,獲得認證

開始
廣告