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

จัดสไตล์ <p> ที่เป็นลูกนับจากล่างด้วย :nth-last-child(n)

ใช้ CSS Selector :nth-last-child(n) เพื่อจัดสไตล์ <p> ที่เป็นลูกลำดับที่ n นับจากลูกสุดท้ายของ Parent

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        p:nth-last-child(4) {
            background: blue;
            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 3") จะมีพื้นหลังสีน้ำเงิน ตัวอักษรสีขาว