CSS Custom Properties or CSS Variables

It is possible to create and use variables within CSS. These could be used to declare a value once and then to reuse that value throughout a large site.

As an example, a web site author could define primary site colors using variables, then simply reference these variable names throughout the rest of the site. This would only be a benefit for large sites where the color codes are used repeatedly.

Neat benefit: Changing the entire color scheme would then become a matter of changing ONLY the variable values.

There are no error checking routines, so the author must be cautious.

Example

Create a variable named main-bg-color and set the value of that variable to blue. Note the use of the :root pseudoclass.

:root {
    --main-bg-color: blue;
}

The naming convention is pretty specific: variable names must begin with two hypens like so: --.

Recall and use the value of the variable.

body {
    background-color: var(--main-bg-color);
}