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

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

ใช้ :root เพื่อจัดสไตล์ Root Element ของ Document (ใน HTML คือ <html>)

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        :root {
            background: blue;
            color: white;
        }
    </style>
</head>
<body>
    <h1>Heading</h1>
    <p>This is demo text.</p>
</body>
</html>

ประโยชน์หลัก: ใช้กำหนด CSS Custom Properties (Variables) ให้ใช้ทั่วทั้ง Document

:root {
    --main-color: #3498db;
    --spacing: 1rem;
}