It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.
You can pass a second value to the
var() CSS function which acts as a fallback for when the property has not been set.
background-color: var(--not-set, #000);
/* Result: #000 background */
The fallback can also be a custom property (with its own fallback).
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.
/* 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.
/* Result: transparent background */
See on CodePen.
- CSS Custom Properties Fail Without Fallback
- Day 28: custom properties and web components
- Day 29: !important custom properties
- Day 41: custom properties and url()s
- !important on MDN
Do you want to learn even more awesome CSS?
I'm running a workshop with my friends at Smashing Magazine in which I introduce you to the most useful modern features in CSS and show how you can implement them today in your code base to improve scalability, maintainability, and productivity.
Learn more about the workshop!