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

เลือก <p> ตัวที่ 2 นับจากล่างด้วย :nth-last-of-type(n)

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

ตัวอย่าง

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

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