Why I'm Excited About Native CSS Variables
philipwalton.com
philipwalton.com
I think this is the problem people have with them - they don't try to be a good solution to the problem people actually have. A better designed solution would have the most common case simple and elegant, and more esoteric use-cases, like the ones they are trying to solve, may have esoteric solutions like the proposed one.
They really are a marvellous thing; my favourite example would be the construction of truly responsive interfaces, tweaked for optimality at many levels and continuous, rather than just things with three or four breakpoints; with preprocessors, it’s tedious and painful; with CSS variables it’s easy and obvious—it pushes you in a direction that is so much easier to make.
In essence, they are solving a substantially different set of problems, but one that is more useful and more general; the sorts of things people did with preprocessor variables can normally be done in better ways with CSS Variables; it helps guide you to a better position.
Anything with preprocessors is tedious and painful, yet I've been hand-writing fluidly responsive layouts for years without predefined 'breakpoint' jumps in the design. What is it about CSS variables that allowed you to change your style?
While it'll be fun to manipulate CSS properties using JS values, it doesn't seem necessary.
JavaScript has shown us the value of designing new features in a way that they can be polyfilled or emulated via a preprocessor, so that features can gain adoption without developers having to wait for older browsers to die.
CSS variables were overdesigned in a way that makes this upgrade path impossible.
Funny you should mention that, I have such a polyfill to introduce Element Query syntax to all browsers IE8 and up, and one of the features we just added was the ability to use variables in your CSS supplied from JavaScript running on the page.
Demo: http://codepen.io/tomhodgins/pen/MKWOWW
Oh, and we also added a $parent selector to CSS, just for giggles :)
http://github.com/eqcss/eqcss < it's under MIT so feel free to use it and build on it
Also, for what it's worth, not all new JavaScript features are polyfill-able. Proxies, for example, are impossible to polyfill, should they not have been added? WeakMaps are also only partially polyfill-able, are they overdesigned too?
There is a huge demand for constants/variables in CSS, as well as a widespread use of preprocessors. The CSS variable spec does bring a lot of interesting ideas to the table, but it does so at the expense of ignoring how people are shipping code today.
Designing a solution that can be dropped into an existing pipeline can be valuable even if doesn't introduce any new concepts or features. For example Promises in JS are pretty simple and just replicate the functionality of existing libraries, but they normalized existing patterns and made it possible to trust that third party code would have consistent behavior. That was a huge boost.
(I wish people would use `calc()` too where appropriate.)
You can reference one line of JS in your CSS style, and the returned value is what is output in your CSS at the time the (responsive) style applies. This value can be a Js variable set elsewhere on the page, the output of a function, or a string or single line of JavaScript that is evaluated in-place.
The var() feature is new and only in the v1.0.0 branch right now, but to read about the other features of this plugin check out https://github.com/eqcss/eqcss and http://elementqueries.com
Just to be clear, neither I nor Addy Osmani proposed CSS Custom Properties. We're just excited about the feature and sharing it with others. So when people respond negatively to us, it doesn't feel like debating.
The time for debating was when it was being discussed in the working group; before it was implemented in three different browsers.
So, you should be immune to a negative response when you publicly state your support for a feature? People don't like how the feature is implemented and say as much. Like I said, it depends on how you define debate and I guess it doesn't match your definition. That's fine with me. But that doesn't mean I don't get to refer to it as a debate. I see it as a debate and if you disagree, that's fine with me as well.
People did debate during the proposal and as such things usually go, people will continue to do so. People had similar issues with flex and gradients. Note that the syntax for those were altered after they were implemented into browsers.