設定每個

元素樣式,其中元素是第 n 個

其父元素的元素


使用 CSS :nth-of-type(n) 選擇器設定每個<p>元素樣式,其中<p>元素是其父元素的第 n 個<p>元素。你可以嘗試執行以下程式碼來實現 :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 次瀏覽

啟動你的職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.