Update (October 20th, 2022): while this helper remains cool, I have decided to not use it outside of personal projects. Compared to IDE auto-comple...
For further actions, you may consider blocking this person and/or reporting abuse
Can you explain why you use css variables instead of scss variables? What are the benefits of using css variables over scss variables?
CSS variables end up being used on the compiled CSS stylesheet. Having CSS variables instead of regular hardcoded values have two benefits:
SCSS variables are intended to help you during your development process. At the end, there’s nothing wrong about sticking to SCSS variables. CSS variables are cool because they allow to do stuff in an easier way.
Recently, I drafted a before/after image comparison with the help of CSS variables. It’s only like 10 lines of JS.
Nowadays, the only good reason to not use CSS variables (apart from “I have to learn them”, I only did recently) is that legacy browsers (IE 11 and Opera Mini) doesn’t support them.
Thanks for the explanation. I was just about to ask the same question. I've been using SASS for years now and it didn't occur to me about the ability to change CSS variables on the fly, that does make sense now that you've explained it.
I've never had cause to use them so I wasn't entirely sure of the point of having to define them in CSS in the first place.
You should add this to the article as a section titled “but why?”
Thanks for the suggestion! It took me some times to know where to add it in order to not break the reading flow. I end up putting it in the first sentence as a link to the comment.
That works good. But i think your comment is specifically regarding favoring CSS variables over SCSS/pre-processed variables.
For me as a reader and someone that is not a css expert, I though “why the hell would you use css variables and make a wrapper around them when pre-processed variables are perfect”
But then I read this comment and it made sense. So I would probably add a section titled “why the heck would you use css variables in SCSS” and link the comment so the reader has context.
I see what you mean. I'll read again the whole stuff when I'll have some time and reconsider the place of the why explanation. Thanks for your feedback!
Nice! Thanks!
They are different things. Scss vars are compiled into values, CSS vars are properties (they're actually called custom properties), that can be changed in runtime, for example by us or by media queries. That gives you other options for theming.
Hi. Is it necessary to use interpolation e.g.
#{$fallback}
, for the fallback? I think it would work without thatYou’re right, thanks! I opened a PR to make sure not to forget.
Merged and it’s even better. \o/
Hello everyone,
I turned this function into a small NPM package:
npm install v.scss
pulls the package into your project.@import '~v.scss';
in a SCSS files makes v() available.Hey, cool way.
I use a different approach, I set all my scss variables in maps, so I can link them to a function and return the css variables. I have a flag as well in case I want to use the real value.
vinceumo.github.io/devNotes/sass/2...
Can your function be rewritten like this?
I just noticed the title of my article may lead to confusion: it’s more about accessing to the variable once defined, it’s not about the variable definition, which is a topic covered by your article.
What you write reminds me the css-vars package or the postcss-custom-properties plugin, which both seems very similar to what you do.
I’m currently working on a SCSS workflow for Custom Media Queries, this time more on the media query definition. It will leverage the postcss-custom-media and postcss-media-minmax plugins, and from what I’m testing, it’ll be the faster way to both declare and use media queries I’ve ever encounter. (Huge statement. :D)
Yo everyone. I updated the article and the
v()
to handle the CSS var() second parameter, which is optional. I didn’t even knew its existence. I hope you’ll enjoy the embed Codepen! 🎶sensible way.
I added a note about a custom property with a weird name:
--
.A bit useless since this function doensn’t allows you to pass a fallback chain as the var() do
Now it does. 👐