This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović
You can pass a second value to the var()
CSS function which acts as a fallback for when the property has not been set.
Fallbacks
div {
background-color: var(--not-set, #000);
}
/* Result: #000 background */
The fallback can also be a custom property (with its own fallback).
div {
background-color: var(--not-set, var(--also-not-set, #00F));
}
/* Result: #00F background */
When Fallbacks fail
If you're not working with custom properties and you set a valid value for a property followed by another declaration with an invalid value, the second declaration will be ignored.
div {
background-color: #F00;
background-color: blahaha;
}
/* Result: #F00 background */
When the value in the second declaration is a custom property that doesn't exist, the declaration is not ignored. Either the property’s inherited value or its initial value, depending on whether the property is inherited or not, will be used instead.
div {
background-color: #F00;
background-color: var(--not-set);
}
/* Result: transparent background */
My blog doesn't support comments yet, but you can reply via blog@matuzo.at.
This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

Manuel Matuzović | Sciencx (2022-09-26T00:00:00+00:00) Day 1: custom properties and fallbacks. Retrieved from https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.