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

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

ใช้ :nth-child(n) เพื่อจัดสไตล์ <p> ตัวที่ n ของ Parent (นับลูกทุกประเภท)

ตัวอย่าง

<!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>

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

รูปแบบ n: ตัวเลข, odd/even, สูตร (3n, 2n+1, เป็นต้น)