DEV Community

Cover image for What no one told you about CSS Variables

What no one told you about CSS Variables

Temani Afif on April 08, 2021

CSS Variables are great but do you know everything about them? In this post, I will highlight a few quirks around CSS variables that no one talks...
Collapse
 
shadowfaxrodeo profile image
Nathaniel

Love the invisible characters thing.
Just worked out using vars for prefixed properties can save a lot of data, e.g.:

.pattern {
    --stripes: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3Cpattern id='s' width='4' height='4' viewBox='0 0 4 4' patternUnits='userSpaceOnUse' patternTransform='rotate(45)'%3E%3Cpath d='M -1 2 h6 v2 h-6z'/%3E%3C/pattern%3E%3Crect fill='url(%23s)' height='100%25' width='100%25'/%3E%3C/svg%3E");
    -webkit-mask-image: var(--stripes);
    mask-image: var(--stripes);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
grahamthedev profile image
GrahamTheDev • Edited

Loved the last one about using emojis as CSS variable names. I was today years old when I learned about that one!

I can imagine someone has already done this but a library where all properties are emojis would be pretty funny, completely unusable, but funny!

Number 4 is one I have been using extensively, you are right that one is a real life saver if you want to bind the :root CSS var to a slider for example.

Great list and beautifully written, easy to follow! ❤🦄!

Collapse
 
icecoffee profile image
Atulit Anand

Never heard of empty variables before.
I'm curious what can be use cases of it.
Thanks for the article mate.
Always something new to learn.

Collapse
 
siddharthshyniben profile image
Info Comment hidden by post author - thread only accessible via permalink
Siddharth • Edited

There's a lot of stuff you can do with it (if/else, AND, OR, NOR, XOR). I'm thinking of writing an article on this.

Edit: here is the article

Collapse
 
krishdevdb profile image
Krish Gupta

I think you misspelled linear-gradient as linaer-gradient at some places

Collapse
 
afif profile image
Temani Afif

it's done on purpose

Collapse
 
krishdevdb profile image
Krish Gupta

But Why?? Must be any reason

Thread Thread
 
afif profile image
Temani Afif

read the article to understand :)

Thread Thread
 
krishdevdb profile image
Krish Gupta

Understood but I didn't understand how they make invalid valid?

Thread Thread
 
afif profile image
Temani Afif

You can see that the browser is no more crossing the property and using the previous one. An invalid value will be ignored by the browser which is not the case here. It's considered valid and only at "computed time" the value will be invalid

Collapse
 
nochance13 profile image
NoChance13

Great article! You did great with explaining the complexities of vars.

One note, it's spelled "unitless" not "uniteless". :-)

Collapse
 
shahednasser profile image
Shahed Nasser

Pretty interesting, especially #3. I imagine if you discovered this one because of a situation happening to you, then it must have been a nightmare to debug it!

Collapse
 
afif profile image
Temani Afif

Indeed, it's a nightmare to debug especially when you have a code like the example I am showing in #4 where all the properties are defined with CSS variables and no hint from the browser which one is not good 😕

Collapse
 
riyanagueco profile image
Riri

Well now I wanna use emojis for my variable names. 😂 Great article!

Collapse
 
yaireo profile image
Yair Even Or

Adding one extra thing to your awesome list:

dev.to/yaireo/optional-css-transfo...

Collapse
 
siddharthshyniben profile image
Info Comment hidden by post author - thread only accessible via permalink
Siddharth

The empty variables thing is really useful. You can learn more here

Some comments have been hidden by the post's author - find out more