ฟังก์ชัน var() ใน CSS ใช้สำหรับเพิ่มค่าคุณสมบัติที่กำหนดเอง (Custom Properties) ลงในเว็บเพจ ตั้งชื่อคุณสมบัติและกำหนดค่าให้มัน
ตัวอย่าง
คุณสามารถลองรันโค้ดต่อไปนี้เพื่อนำฟังก์ชัน var() ไปใช้ได้
<!DOCTYPE html> <html> <head> <style> :root { --my-bg-color: blue; --my-color: white; } #demo { background-color: var(--my-bg-color); color: var(--my-color); } </style> </head> <body> <h1>Heading One</h1> <div id = "demo"> This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. </div> <br> </body> </html>