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
- Guaranteed-invalid value
- Applies To
- All elements
- Inherited?
- Yes
- Media
- All
- Computed Value
- Specified value with variables substituted
- Animatable
- Yes (as discrete, or as specified by the
@propertyrule)
CSS Specifications
Custom properties are defined in the CSS Custom Properties for Cascading Variables Module Level 1 (W3C Candidate Recommendation Snapshot).
Browser Support
Browser support for CSS properties can vary, particularly for newer or experimental features. Check current browser compatibility before using a property in production, especially when supporting older browsers.
The following table provided by Caniuse.com shows the level of browser support for this feature.
Initial Value
CSS properties have an initial value that applies when the property has not been explicitly set. The initial value is different from the value inherited from a parent element and may vary between properties.
Inheritance
Some CSS properties inherit their computed value from their parent element by default, while others do not. The inherit, initial, unset, and revert keywords can also be used to control how a property gets its value.
Vendor Prefixes
Most modern CSS properties use the standard, unprefixed syntax. Vendor-prefixed properties such as -webkit- are mainly relevant to older browser versions or specific features and should only be used when required by your browser support targets.