This is it. This is all I’ve wanted in CSS for the last decade. In fact, I just wanted basic mixins without function-like behavior, but this gets me there.
The comments here indicate that the article doesn’t do much to explain what this does or why you’d want it, but basically this gives you a way to reuse blocks of styles without needing to resort to e.g. utility classes or other hacks that get around specificity issues. The syntax doesn’t help explain that.
Shame about the syntax, but I’m used to it at this point.
Thanks. Looks hideous.
@function --light-dark(--light, --dark) {
result: var(--light);
@media (prefers-color-scheme: dark) {
result: var(--dark);
}
}
Was https://en.wikipedia.org/wiki/JavaScript_Style_Sheets right all along?
Oh man, and I thought the current JS status-quo was already full of vulnerabilities and privacy risks.
If I had mind-boggling amounts of money, one of the things I would do is start an organization to name and shame and give anti-awards to websites that most abuse javascript and whatever-this-will-be. Or a dang mutual fund: "Like an index fund except we don't support companies with websites that require a ridiculous amount of control over the client computer."
Wait, who am I kidding: A real activist tycoon would probably just become a shareholder and make demands.
I look forward to writing Typescript that transpiles to Javascript that writes CSS that calls functions.
This feels like a really bad idea, I hope it doesn't make it into the browser. CSS Houdini would be a better path to take.
It seems like the spec draft itself offers a much better example than the article.
https://drafts.csswg.org/css-mixins-1/
I do think it's strange that using a wrapper like var() wasn't required here. Like func() or such. I'd actually rather that var() hadn't existed to begin with, but it should be consistent, at least.
Taking bets now on how long after release it takes to end up with someone making a "JS in CSS" framework, but with the added wrinkle that they have to be serious about it, not just doing it as a joke.
So taking the article seriously and not just being glib:
This feels like a misunderstanding of how to write good CSS. Good CSS is a lot like SQL, which shouldn't be a surprise since it's centered around selectors. CSS is a set-based language. Adding general purpose functions that seem to abuse rule precedence to make conditional statements feels like a dirty hack to force CSS into an imperative model.
Now, maybe there is something to be said for the idea that, here we are, 30 years into having CSS, and people are still bad at set-description operations. Maybe there should be an imperative styling language. But it's a mistake to overload CSS to do it.
Why stop there? Why not add interfaces, OO and design patterns while we're at it? Then CSS will be a "real" language.
I'm confused at all the negativity? I am personally very excited for this feature, it's going to enable one to use less JavaScript,and CSS to accomplish things that should be easy.
Today on "everything that's wrong with modern web and more"
This is yet another example of how declarative languages almost always evolve into full programming language if popular enough.
Usually with really bad syntax because the original design didn't expect that to happen.
I don't know how the web does it. There are so many cool things being added. How do they keep the spec manageable? What if someone wanted to build a compliant web browser from the ground up?
Finally, the inverse of CSS-in-JS.
Looking forward to DoomCSS
OK but how about they fix broken CSS variables first before this?
DOOM in CSS when?
Why not just use CSS-in-JS at this point? With functions and if statements, you now have two full Turing-complete programming languages in the browser.
Why? If the problem is JS bundle parse time, couldn't you just have a special kind of <script> tag that opts out of compilation and runs in interpreted mode only, and stick your layout JS in there? Having two entirely separate languages seems like such a waste of effort both on the platform side, and on the web developer side having to learn so many things.
"Your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should."
Is it time for "CSS: The Good Parts" ?
functions in stylesheets?
That's nuts.
The web is developing in the wrong way.
oh the horrible css syntax.... brrrr
I wish we'd just admit we're heading for SASS and use that. It'd save a lot of hand-wringing and time spent justifying adding each feature.
There is so little to write about and so much to overengineer about our text systems that all we can do now is endlessly debate punctuation.
Why do we allow Google to singlehandedly keep pumping garbage into the web ecosystem? Do they own W3C?
The syntax might kind of suck but this will actually be very useful to have.
holy shit I need to retire soon
No worries, the web is already went wrong from the original purpose. Google is outdated itself. Better they update their own blog spot site layouts/functions before touching the web.
This is exactly the sort of propaganda which encourages making sites that don't work in any browser but Google's. If web developers would just focus on making simple and widely-accessible sites instead of churnful trendchasing, the browser monopoly wouldn't exist.
Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google.
Exactly.