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

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


Pseudo-class :only-child

ใช้ :only-child เลือก <p> ที่เป็นลูกเพียงตัวเดียวของพาเรนต์ (ไม่มีพี่น้อง)

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <style>
    p:only-child {
      background: orange;
    }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <div>
    <p>This is a paragraph.</p>
  </div>
  <div>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
  </div>
</body>
</html>