CSS --*

In CSS, the --* syntax defines a custom property. It defines the substitution value of var() functions.

A custom property can be thought of as a variable. Custom properties allow you to define a value in one place, then reuse that value elsewhere in your style sheets, simply by using the name of the custom property.

Custom properties can be handy when you need to use the same property values in many different parts of your style sheet. Rather than duplicate that value across multiple parts of the style sheet, you can use a custom property instead. If you need to update the value, you only need to do it in one place.

The --* syntax is what you must use when you define a custom property. The asterisk (*) is a wildcard for other characters.

Custom properties are defined as any valid identifier that starts with two dashes (U+002D HYPHEN-MINUS). For example, a custom property could be called --main-color. You could then refer to that custom property by passing that value to the var() function.

Case-Sensitivity

It's important to note that custom properties are case-sensitive. This is in contrast to regular CSS properties, which are case-insensitive.

Therefore, --main-color and --Main-Color are two different properties.

Here's an example that demonstrates this.

This is exactly the same as the first example above, except that I'm now using the wrong case when referencing the custom properties.

Because of this, the var() function has no effect.

However, if I used a fallback value, then the fallback value would be used.

Fallback Values

When using the var() function, you can optionally provide a fallback value to be used in case the custom property is invalid.

Here's an example of using a fallback value.

In this case, the custom properties weren't even defined, so the var() function used the fallback values.

Basic Property Information

Initial Value
(Nothing)
Applies To
All elements
Inherited?
Yes
Media
All
Computed Value
Specified value with variables substituted
Animatable
No

CSS Specifications

Custom properties are defined in the CSS Custom Properties for Cascading Variables Module Level 1 (W3C Candidate Recommendation, 03 December 2015)

Browser Support

The following table provided by Caniuse.com shows the level of browser support for this feature.

Vendor Prefixes

For maximum browser compatibility many web developers add browser-specific properties by using extensions such as -webkit- for Safari, Google Chrome, and Opera (newer versions), -ms- for Internet Explorer, -moz- for Firefox, -o- for older versions of Opera etc. As with any CSS property, if a browser doesn't support a proprietary extension, it will simply ignore it.

This practice is not recommended by the W3C, however in many cases, the only way you can test a property is to include the CSS extension that is compatible with your browser.

The major browser manufacturers generally strive to adhere to the W3C specifications, and when they support a non-prefixed property, they typically remove the prefixed version. Also, W3C advises vendors to remove their prefixes for properties that reach Candidate Recommendation status.

Many developers use Autoprefixer, which is a postprocessor for CSS. Autoprefixer automatically adds vendor prefixes to your CSS so that you don't need to. It also removes old, unnecessary prefixes from your CSS.

You can also use Autoprefixer with preprocessors such as Less and Sass.