To use a custom property, you substitute a regular CSS property’s value with a CSS variable based on that custom property. You create that variable by using the var()
function:
[var]property[/var]: var(--[var]custom-property-name[/var] [, [var]fallback-value[/var]]);
--
).
For example, recall my --accent-color
custom property from the previous section:
:root { --accent-color: hsl(102, 37%, 75%); }
I can now replace each instance of hsl(102, 37%, 75%)
in my stylesheet with a CSS variable:
header { background-color: var(--accent-color, orangered); } nav { color: var(--accent-color, orangered); } button { background-color: var(--accent-color, orangered); } aside { border-left: 1px solid var(--accent-color, orangered); } footer { background-color: var(--accent-color, orangered); }
I've added these declarations to the CSS Editor. Try changing the value of the --accent-color
custom property to see for yourself the power and convenience of CSS variables.