Re:CSS now has an if() conditional function
by bradly on Friday December 05, 2025 @12:20PM
(#46164282)
I recently implemented dark/light mode for the first time and was really surprised to find that in order to add a toggle I had to duplicate a both of vars/styles and use JavaScript. I'm looking forward to not having to deal with that cruft in the future.
Re:CSS now has an if() conditional function
by hebelehubele on Friday December 05, 2025 @03:35PM
(#46166908)
You can do without javascript. Checkbox `:checked` + `label` trick for toggling states still works.
Re:CSS now has an if() conditional function
by bradly on Friday December 05, 2025 @08:55PM
(#46169811)
Thanks, I’ll check this out!
Re:CSS now has an if() conditional function
by hebelehubele on Saturday December 06, 2025 @05:10AM
(#46172118)
Here's a version with a 3-way theme selector (light / system / dark) using radio inputs.
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.