CSS var() Function
- Previous Page CSS url() function
- Next Page CSS acos() function
- Go to the Previous Level CSS Function Reference Manual
Definition and Usage
CSS's var()
Function to insert the value of CSS variables.
Example
Example 1
First, declare a global variable named --main-bg-color, and then use it in the stylesheet. var()
Function to insert the value of the variable:
:root { --main-bg-color: coral; } #div1 { background-color: var(--main-bg-color); } #div2 { background-color: var(--main-bg-color); }
Example 2
Another example using var()
Example of inserting multiple CSS variable values with a function:
:root { --main-bg-color: coral; --main-txt-color: blue; --main-padding: 15px; } #div1 { background-color: var(--main-bg-color); color: var(--main-txt-color); padding: var(--main-padding); } #div2 { background-color: var(--main-bg-color); color: var(--main-txt-color); padding: var(--main-padding); }
CSS Syntax
var(--name, value)
Value | Description |
---|---|
--name | Required. Variable name (must start with two dashes). |
value | Optional. Alternative value (to be used if the variable is not found). |
Technical Details
Version: | CSS3 |
---|
Browser Support
The numbers in the table indicate the browser version that first fully supports this function.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
49 | 15 | 31 | 9.1 | 36 |
Related Pages
Tutorial:CSS Variables
- Previous Page CSS url() function
- Next Page CSS acos() function
- Go to the Previous Level CSS Function Reference Manual