CSS Variables
- Previous Page CSS User Interface
- Next Page CSS Overriding Variables
CSS Variables
The var() function is used to insert the value of a CSS variable.
CSS variables can access the DOM, which means you can create variables with local or global scope, use JavaScript to change variables, and change variables based on media queries.
A good way to use CSS variables involves design colors. You can put them in variables without having to copy and paste the same colors over and over again.
Traditional way
The following example shows the traditional way to define some colors in a style sheet (by defining the color to be used for each specific element):
Example
body { background-color: #1e90ff; } h2 { border-bottom: 2px solid #1e90ff; } .container { color: #1e90ff; background-color: #ffffff; padding: 15px; } button { background-color: #ffffff; color: #1e90ff; border: 1px solid #1e90ff; padding: 5px; }
The syntax of the var() function
The var() function is used to insert the value of a CSS variable.
The syntax of the var() function is as follows:
var(name, value)
Value | Description |
---|---|
name | Required. Variable name (starting with two dashes). |
value | Optional. Fallback value (used when the variable is not found). |
Note:Variable names must start with two dashes (--), and are case-sensitive!
How var() works
Firstly: CSS variables can have global or local scope.
Global variables can be accessed/used throughout the document, while local variables can only be used within the selector where they are declared.
To create a variable with global scope, declare it in the :root selector. The :root selector matches the root element of the document.
To create a variable with local scope, declare it in the selector where it will be used.
The following example is the same as the above example, but here we use the var() function.
Firstly, we declare two global variables (--blue and --white). Then, we use the var() function to insert the values of the variables later in the style sheet:
Example
:root { --blue: #1e90ff; --white: #ffffff; } body { background-color: var(--blue); } h2 { border-bottom: 2px solid var(--blue); } .container { color: var(--blue); background-color: var(--white); padding: 15px; } button { background-color: var(--white); color: var(--blue); border: 1px solid var(--blue); padding: 5px; }
The following are the advantages of using var():
- Make the code easier to read (easier to understand)
- Make it easier to modify color values
If you want to change the blue and white to softer blue and white, you just need to modify the values of two variables:
Example
:root { --blue: #6495ed; --white: #faf0e6; } body { background-color: var(--blue); } h2 { border-bottom: 2px solid var(--blue); } .container { color: var(--blue); background-color: var(--white); padding: 15px; } button { background-color: var(--white); color: var(--blue); border: 1px solid var(--blue); padding: 5px; }
Browser Support
The numbers in the table indicate the first browser version that fully supports this property.
Function | |||||
---|---|---|---|---|---|
var() | 49.0 | 15.0 | 31.0 | 9.1 | 36.0 |
CSS var() Function
Function | Description |
---|---|
var() | Insert the value of the CSS variable. |
- Previous Page CSS User Interface
- Next Page CSS Overriding Variables