Day 1: custom properties and fallbacks

It’s time to get me up on 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.

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 */


This content originally appeared on Web development blog - Manuel Matuzović and was authored by Manuel Matuzović

It’s time to get me up on 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.

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 */


This content originally appeared on Web development blog - Manuel Matuzović and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-09-26T09:38:54+00:00) Day 1: custom properties and fallbacks. Retrieved from https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/

MLA
" » Day 1: custom properties and fallbacks." Manuel Matuzović | Sciencx - Monday September 26, 2022, https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/
HARVARD
Manuel Matuzović | Sciencx Monday September 26, 2022 » Day 1: custom properties and fallbacks., viewed ,<https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 1: custom properties and fallbacks. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/
CHICAGO
" » Day 1: custom properties and fallbacks." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/
IEEE
" » Day 1: custom properties and fallbacks." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/. [Accessed: ]
rf:citation
» Day 1: custom properties and fallbacks | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/09/26/day-1-custom-properties-and-fallbacks/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.