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

บทบาทของ CSS :first-of-type Selector

ใช้ :first-of-type เพื่อจัดสไตล์ <p> ตัวแรกของ Parent (นับเฉพาะ Tag <p> เท่านั้น)

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        p:first-of-type {
            background: orange;
        }
    </style>
</head>
<body>
    <p>This is demo text1.</p>
    <p>This is demo text2.</p>
</body>
</html>

ต่างจาก :first-child: :first-child ต้องเป็นลูกคนแรกของ Parent ทุกประเภท ส่วน :first-of-type เป็น Tag นั้นๆ ตัวแรกเท่านั้น