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

จัดสไตล์ p ลูกคนที่ 2 ของพาเรนต์ด้วย CSS


Pseudo-class :nth-child(n)

ใช้ :nth-child(n) เลือกองค์ประกอบที่เป็นลูกคนที่ n ของพาเรนต์ (นับทุกชนิด)

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <style>
    p:nth-child(4) {
      background: orange;
      color: white;
    }
  </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>
  <p>This is demo text 5.</p>
  <p>This is demo text 6.</p>
</body>
</html>