TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

CSS Variables in Firefox Nightly

186 pointsby daw___over 11 years ago

22 comments

pornelover 11 years ago
Please note that CSS Variables are not like SASS&#x2F;LESS variables. They&#x27;re actually <i>custom properties</i> that obey inheritance.<p>They&#x27;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.<p>Example from the spec:<p><pre><code> :root { var-color: blue; } div { var-color: green; } #alert { var-color: red; } * { color: var(color); } &lt;p&gt;I inherited blue from the root element!&lt;&#x2F;p&gt; &lt;div&gt;I got green set directly on me!&lt;&#x2F;div&gt; &lt;div id=&#x27;alert&#x27;&gt; While I got red set directly on me! &lt;p&gt;I’m red too, because of inheritance!&lt;&#x2F;p&gt; &lt;&#x2F;div&gt;</code></pre>
评论 #6913439 未加载
评论 #6911316 未加载
transfireover 11 years ago
W3C strikes again. They are uniquely gifted at creating ungainly and ugly standards even in the face of widespread industry practices are already commonly used (less, sass). I really want to know who thought `var-` prefix was a banging idea.
评论 #6910119 未加载
评论 #6910264 未加载
评论 #6910113 未加载
评论 #6910382 未加载
bbxover 11 years ago
CSS variables are the only reason I started using Sass and LESS in the first place. But although these preprocessors come with other interesting features, I never managed to truly incorporate them in my workflow.<p>The way I ended up solving the CSS variables issue (i.e. replacing the same property&#x2F;value combination in multiple locations) was to actually regroup <i>selectors</i>.<p>For example, I&#x27;d have:<p><pre><code> #title, .nav a, .post-title em { color: #db4e44; } </code></pre> The only annoying thing is that it can result in a long list of selectors (although I&#x27;d put them on one line anyway). But one thing I truly appreciate with this approach is that you can edit <i>any</i> value with your browser&#x27;s inspector, and see it <i>instantly</i> update all instances at once. It&#x27;s great to test out new colors for example.<p>The other option would be to have a specific class that would apply this style. But then, you&#x27;d move the styling from the CSS to the HTML, and I&#x27;ve always tried to prevent myself from breaking this golden rule.
评论 #6910050 未加载
评论 #6909959 未加载
评论 #6909755 未加载
评论 #6910048 未加载
评论 #6913183 未加载
评论 #6910128 未加载
评论 #6909874 未加载
sergiotapiaover 11 years ago
Really ugly syntax.<p><pre><code> var-* ???? </code></pre> Who thought this would be a good idea?
评论 #6910329 未加载
评论 #6919071 未加载
评论 #6910174 未加载
评论 #6910823 未加载
georgecalmover 11 years ago
Does anyone know if there&#x27;s a polyfill for CSS variables out there? Possibly a library that creates plain old CSS out of the one that uses variables. It would be useful until this is rolled out to other browsers.
评论 #6910437 未加载
评论 #6911764 未加载
评论 #6909807 未加载
chimeover 11 years ago
I like that you can overwrite the var(headersize) in a different style. That makes these pretty powerful. However, var(headersize) is a lot to type, compared to @headersize (like LESS) or $headersize (like SASS). CSS allows for a lot of shorthand (#AABBCC = #AAA, table tr td a ~= table td a, * {}). Would be nice if there was a shorthand for variables too since they&#x27;ll be used so much.
评论 #6909869 未加载
yashauover 11 years ago
Note that you can also do this in Chrome&#x2F;Chromium by turning on the &quot;Enable experimental Web platform features&quot; flag.
dreamdu5tover 11 years ago
This is disgusting. &quot;var-&quot; prefixes!? No. Just no.<p>Now there&#x27;s another reason to use Rework, Stylus, LESS, or SASS.
评论 #6911783 未加载
oddshocksover 11 years ago
FINALLY people fixing the tools instead of creating wrappers around them! Thank you Mozilla!
评论 #6910320 未加载
mekishizufuover 11 years ago
The downside of this (unless I&#x27;m missing something) is that these variables need to be evaluated on the client side and inevitably slow down CSS processing (at least for the first time that is). I really like the fact that you can preprocess both LESS and Sass (and others) to just plain CSS and hide all of the dynamic behavior (far more then just variables).
评论 #6909963 未加载
danabramovover 11 years ago
By the way, [AbsurdJS][1] is an interesting alternative to Sass&#x2F;LESS. It lets you write your CSS in JavaScript (and have it compiled to CSS). Thus, you get mixins, variables and plugins without some DSL to learn&#x2F;use&#x2F;depend on—all written in JavaScript.<p><pre><code> [1]: http:&#x2F;&#x2F;krasimir.github.io&#x2F;absurd&#x2F;</code></pre>
评论 #6910266 未加载
xuxover 11 years ago
Wow CSS is becoming more and more like a programming language. Good job on Firefox for pushing the boundary.<p>One thing though. The calculations don&#x27;t seem very semantic. To calculate something as simple as height&#x2F;width, you need a long calc(var(height)&#x2F;var(width));<p>Maybe w3 can implement CSS variables with special characters like in PHP.
评论 #6910543 未加载
simonrobbover 11 years ago
I just can&#x27;t see what the path to adoption of this looks like. How do you achieve backward compatibility?<p>You put a box-shadow on your elements today and while it won&#x27;t look quite as shiny in browsers that don&#x27;t support it, it&#x27;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&#x27;t understand the directive? If my elements aren&#x27;t blue in anything except the latest build of FF, there&#x27;s no way I can justify using the feature, obviously (as much as I like the idea of it).
评论 #6911747 未加载
drawkboxover 11 years ago
It would be great if there was a new standard like LESS&#x2F;SASS for on the fly browser CSS creation. Think of the download speed up and the simplicity, removing minifiers, bandwidth, complexity. Also when new browser support elements are added the intermediary layer will just be the global one always even during development. Browsers will just add them to this instead of prefixed properties moz- etc. Support can change with the same simple stylesheets, browsers worry about it.
drinchevover 11 years ago
I&#x27;m not sure why, but reading this news is not so exciting. SASS&#x2F;LESS are basically a standard ( along with Compass and other ), so I&#x27;m not sure how CSSWG will get out of the mess they made with &quot;-moz,-webkit,-o&quot; and many, many others ... Native support of CSS variables is like querySelectorAll. You got it, but you still use jQuery, because of backward &#x2F; forward support.
jccooperover 11 years ago
Nifty. I&#x27;m really looking forward to using those in several years.<p>The need for CSS variables has only been apparent for about forever. What took so long?
tiquorsjover 11 years ago
Sass and less are really a kluge that just sheds light on how much CSS needs this built in.
meeritaover 11 years ago
I don&#x27;t like this.<p>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.<p>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.<p>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.
评论 #6911769 未加载
joelthelionover 11 years ago
What&#x27;s next? CSS loops? Why not make CSS Turing-complete while they&#x27;re at it :)
评论 #6910778 未加载
kyriakosover 11 years ago
would be nice if this eventually turned into something similar to sass or less
ozhover 11 years ago
sooooo much better than SASS or LESS which require additional tools that break workflow.
评论 #6910138 未加载
评论 #6911571 未加载
评论 #6910104 未加载
ape4over 11 years ago
Is CSS Turing complete yet?
评论 #6909891 未加载