DEV Community

Discussion on: CSS Variables (the basics)

Collapse
 
jmdejager profile image
🐤🥇 Jasper de Jager • Edited

Nice article!
Don't forget the beauty of default values for a css variable.

background-color: var(--background-color, midnightblue)

with this you'll make it possible te have a default but override it if you want to.

I also use css variables in combination with calc() sometimes

margin: 20px calc(20px - var(--some-special-width-var, 0px))

Collapse
 
afif profile image
Temani Afif

in your last example you need to use 0px instead of 0 because the unitless value will make the calc() invalid because calc(20px - 0) is invalid (more detail : stackoverflow.com/a/55406181/8620333)

Collapse
 
jmdejager profile image
🐤🥇 Jasper de Jager • Edited

Thnx! Changed it. So easy to forget the details..

Collapse
 
nathanh profile image
Nathan

Thank you! You make a very good point- always good practice to have a default value.