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

บทบาทของ CSS :empty Selector

ใช้ :empty เพื่อจัดสไตล์ <p> ที่ไม่มีลูก (No children) รวมถึง Text Node ด้วย

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        p.demo {
            width: 300px;
            height: 20px;
            background: gray;
        }
        p:empty {
            width: 150px;
            height: 20px;
            background: orange;
        }
    </style>
</head>
<body>
    <p class="demo">This is demo text. Below is empty text.</p>
    <p></p>
</body>
</html>

หมายเหตุ: <p></p> ว่างเปล่าไม่มี Text Node จะถูกเลือก แต่อย่าง <p> </p> (มี Space) จะไม่ถูกเลือก