Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

จัดรูปแบบทุก

องค์ประกอบที่เป็นลูกคนแรกของผู้ปกครองด้วย CSS


หากต้องการจัดรูปแบบทุก

องค์ประกอบที่เป็นลูกคนแรกของพาเรนต์ ให้ใช้ CSS :first-child selector

ตัวอย่าง

คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อใช้งาน :first-child selector -

<!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>