CSS now has an if() conditional function

caniuse.com

132 points by aanthonymax 5 days ago


zkmon - 3 hours ago

Give it enough time, every declarative language becomes a programming language. This is happening with all config files, markup languages, data formats.

The distinction between code, config and data is being erased. Everything is a soup now. Data is application, configuration is code. Code is an intermediate, volatile thing that is generated on the fly and executed in the temporary lambda containers.

chipx86 - 29 minutes ago

I'm pretty happy to see this, as conditionals can really help keep code manageable when trying to define CSS variables or other properties based on combinations of light mode, dark mode, high-contrast, contextual state in a document or component, etc.

if() isn't the only way to do this, though. We've been using a technique in Review Board that's roughly equivalent to if(), but compatible with any browser supporting CSS variables. It involves:

1. Defining your conditions based on selectors/media queries (say, a dark mode media selector, light mode, some data attribute on a component, etc.).

2. Defining a set of related CSS variables within those to mark which are TRUE (using an empty value) and which are FALSE (`initial`).

3. Using those CSS variables with fallback syntax to choose a value based on which is TRUE (using `var(--my-state, fallback)` syntax).

I wrote about it all here, with a handful of working examples: https://chipx86.blog/2025/08/08/what-if-using-conditional-cs...

Also includes a comparison between if() and this approach, so you can more easily get a sense of how they both work.

culi - 8 hours ago

Far from being ready when only one major browser supports it. If you want this, you should vote for it to be focused on for interop-2026

https://github.com/web-platform-tests/interop/issues

Right now, the leading CSS proposals are `@container style()`, `corner-shape` and `break-after`

https://foolip.github.io/interop-reactions/

Aardwolf - 36 minutes ago

Can it already vertically and horizontally center unknown-beforehand-length multi-line text in a single html element, just like non-CSS table cells could already in 1995?

Incipient - 2 hours ago

As long as css remains "so fast it's free" then I'm mostly happy with that - I use css without thinking about optimisations, and I like it like that!

themafia - 8 hours ago

It's a great way to make conditional styles without having to use JavaScript; however, having used JS for years to make theme color and icon sets that rely on CSS properties, I'm not sure I particularly like this method. I feel like you have to smear a lot of logic across your CSS whereas with JS you can reduce your theme to a data structure and just have a simple function to setup all the CSS variables based on that.

Am I just an old man?

nottorp - 2 hours ago

So we were looking in the wrong direction for AGI!

tanin - an hour ago

Nice. I've built desktop apps in a few other frameworks e.g. Java Swing, JavaFX, JetBrains Compose, SwiftUI, QT. Nothing is as easy as JS/HTML/CSS. I've realized that the main reason is its robust capabilities e.g rich auto-layouting/positioning capabilities.

Meanwhile in other UI frameworks, you either don't do it or you draw the damn things yourself lol. So, most of the times I'd just not do it.

Adding if is great. It would reduce the need for JS a bit more, which would make the code more maintainable.

bingemaker - 6 hours ago

I'm not sure turning CSS into a full blown language is a good idea. With all the cascading in place, it is already a bit hard to determine why certain styles are applied. Now with this, we will be spending more brain cells debugging CSS issues.

EmilStenstrom - 3 hours ago

Here is a much better link to how it works: https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/V...

stevefan1999 - 8 hours ago

With the inclusion of branches, is it possible to say that CSS is now even more Turing-Complete? Now we just need to find ways to do recursion/targeted jumps so that it is finally recursive-enumerable

manucardoen - 24 minutes ago

Somewhere, someone has just started porting Doom to CSS.

xnx - 8 hours ago

If we could do it over, knowing that we'd eventually get to this point, would https://en.wikipedia.org/wiki/JavaScript_Style_Sheets have been the better path?

tantalor - 8 hours ago

https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/V...

jsmailes - an hour ago

If I'm reading this correctly, Opera added support in an earlier version then took it away again. Any idea why they might have done it? Maybe a browser engine change under the hood?

zuhsetaqi - 3 hours ago

It's a working draft and only available in Chromium ...

mr_windfrog - 6 hours ago

I'm not really sure I understand this. How is the new if() conditional function different from using @media (width ...) when adapting layouts to browser width?

silverwind - 4 hours ago

This is missing a "if variable equals" imho. Right now it seems like pure syntactic sugar for a media query.

foreigner - 3 hours ago

Can these be used inside inline style attributes?

zb3 - 8 hours ago

Not supported in Firefox and Safari. Also it seems most people forget that the more bloated the web platform is, the more resources are needed to develop and maintain a web browser engine.. Chromium is open-source, but it's already expensive to maintain a fork or even rebuild it..

aanthonymax - 5 days ago

This support has appeared in the new W3C specification.

darig - 5 hours ago

[dead]