Hàm var() trong CSS được dùng để thêm giá trị thuộc tính tùy chỉnh vào trang web. Đặt tên tùy chỉnh cho thuộc tính và gán giá trị cho nó.
Ví dụ
Bạn có thể chạy mã sau để triển khai hàm 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. </div> <br> </body> </html>