CSS 的作用:first-child 選擇器


使用 CSS:first-child 選擇器可以對作為父元素首個子級元素的每個 <p> 標記設定樣式。

示例

可以嘗試執行以下程式碼來實現:first-child 選擇器

即時演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:first-child {
            background-color: orange;
         }
      </style>
   </head>
   <body>
      <h1>Heading</h1>
      <p>This is demo text.</p>
      <div>
         <p>This is demo text, with the first child of its parent div.</p>
         <p>This is demo text, but not the first child.</p>
      </div>
   </body>
</html>

更新於: 2020 年 6 月 30 日

135 次瀏覽

開啟你的 職業生涯

透過完成課程獲取認證

開始學習
廣告