CSS Variables in Firefox Nightly
hacks.mozilla.org
hacks.mozilla.org
They're scoped to document rather than stylesheet, so you can override them in parts of the document just like you can override any CSS property.
Example from the spec:
:root { var-color: blue; }
div { var-color: green; }
#alert { var-color: red; }
* { color: var(color); }
<p>I inherited blue from the root element!</p>
<div>I got green set directly on me!</div>
<div id='alert'>
While I got red set directly on me!
<p>I’m red too, because of inheritance!</p>
</div>However, I think that just copying how mixins and variables work in SCSS work into the CSS4 spec would do wonders. I'm 100% for it
There are often times in my CSS workflow where I want to do something similar to this:
.widget
$padding: 3px
$activeColor: $color1
$inactiveColor: $color2
padding: $padding
/* ... etc. */
.subWidget
$padding: 6px
.subWidget2
$inactiveColor: $color3
/* ... and so on */
With preprocessors, this doesn't work - you're better off defining a mixin that you feed variables into, which isn't the most intuitive approach. I hope this is adopted quickly by the major browser vendors. Unfortunately, writing your styles in this way will completely break in browsers that don't support it, in the absence of some sort of polyfilling JS or build tool.Sound logic, isn't it?
The way I ended up solving the CSS variables issue (i.e. replacing the same property/value combination in multiple locations) was to actually regroup selectors.
For example, I'd have:
#title,
.nav a,
.post-title em {
color: #db4e44;
}
The only annoying thing is that it can result in a long list of selectors (although I'd put them on one line anyway). But one thing I truly appreciate with this approach is that you can edit any value with your browser's inspector, and see it instantly update all instances at once. It's great to test out new colors for example.The other option would be to have a specific class that would apply this style. But then, you'd move the styling from the CSS to the HTML, and I've always tried to prevent myself from breaking this golden rule.
.brand {
color: #bada55;
}
See OOCSS for more details.While that's good practice, it's a challenge to get it right on a large legacy systems, so I still find variables useful in practice. Also, it's nice to be able to perform functions involving variables, e.g.
.brand {
color: lighten($link-color, 20%);
background: ($link-bg + $page-bg)/2;
}Variables are also useful when you can do calculations on them.
I don't know why, if you're using LESS or equivalent, you wouldn't use variables. It makes for vastly more maintainable CSS than the approach you take.
%red
color: #f00
%blue
color: #00f
header
@extend %red
div
@extend %blue
#foo
@extend %red
p
@extend %red
gets processed into header, #foo, p {
color: #F00;
}
div {
color: #00F;
}
codepen sample: http://cdpn.io/JoHAv var-* ????
Who thought this would be a good idea? data-*
is for attributes in HTML. It makes sense for CSS properties, syntactically speaking to map these two in terms of prefix-solutions.[1]: https://github.com/visionmedia/rework-vars [2]: https://github.com/visionmedia/rework
a:link { color: var(main-color); }
.important-quote { var-main-color: red; }
/* <div class="important-quote"> <a href="#">Hi, I should be red;</a> </div> */
Mh.. Well you could actually do some kind of @extend-on-steroids, if you do not care to bloat your styles.Now there's another reason to use Rework, Stylus, LESS, or SASS.
It was really not worth it to create wrappers.
It reads over the definitions ONCE (with all the CSS hierarchy and such), and then establishes it's own internal constant value for that variable. Referring to that value will be no more expensive than referring to a value that is set by the CSS sheets like "FF55FF".
:root, .green.applyColor {
var-color: #00FF00;
}
.red.applyColor {
var-color: #FF0000;
}
.blue.applyColor {
var-color: #0000FF;
}
.applyColor {
color: var(color);
}
With this style sheet, items with the "applyColor" class will default to green, unless they also have the class "blue" or "red", in which case the color will switch appropriately.So how, exactly, is a CSS processor supposed to read this definition once and come up with a single internal constant for the variable "color" that can be looked up in constant time? And how does that offer any advantage over just using its already-programmed cascading rules to properly resolve "var-color" whenever it encounters "var(color)"?
[1]: http://krasimir.github.io/absurd/I do hope we're approaching the end of the era when this is used as a positive statement.
[1] https://developer.mozilla.org/en-US/docs/E4X/Processing_XML_...
One thing though. The calculations don't seem very semantic. To calculate something as simple as height/width, you need a long calc(var(height)/var(width));
Maybe w3 can implement CSS variables with special characters like in PHP.
You put a box-shadow on your elements today and while it won't look quite as shiny in browsers that don't support it, it's not going to affect usability. If you put a variable in that stylesheet, say to make the background of all your clickable elements blue, how do you provide a fallback for the browsers that don't understand the directive? If my elements aren't blue in anything except the latest build of FF, there's no way I can justify using the feature, obviously (as much as I like the idea of it).
If I were using a preprocessor, I'd just use the in-built variables of that preprocessor I guess.
The need for CSS variables has only been apparent for about forever. What took so long?
I prefer the freedom of platforms like LESS or SASS. They can implement changes faster and they can have full set of features without worrying about updates or platforms. They just output standard CSS.
If we relay on browser features, we will be slower, we will tie up to vendor differences and things will get complicate over time to develop something cool, in fact, there will be projects for sure that re-unify what browsers precompile, so, no to this.
I would suggest vendors to support the standard CSS spec to the max, and keep this good work instead going something really new like this.
A native CSS solution is definitely better in an ideal world. But we will still need SASS/LESS/etc for years to come because in the real world old browsers still get used a lot.