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

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


Pseudo-class :only-of-type

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

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <style>
    p:only-of-type {
      background: orange;
      color: white;
    }
  </style>
</head>
<body>
  <div>
    <p>This is demo text 1.</p>
  </div>
  <div>
    <p>This is demo text 2.</p>
    <p>This is demo text 3.</p>
    <p>This is demo text 4.</p>
  </div>
</body>
</html>