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

บทบาทของ CSS :nth-of-type(n) Selector

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

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        p:nth-of-type(2) {
            background: yellow;
        }
    </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>
</body>
</html>

ผลลัพธ์: <p> ตัวที่ 2 ("This is demo text 2") จะมีพื้นหลังสีเหลือง

รูปแบบ n: ตัวเลข (2), คีย์เวิร์ด (odd, even), สูตร (2n+1)