The gap: no generic conditionals in CSS

CSS still lacks a switch rule and a general if, leaving only the narrow conditionals of @media queries and custom-property tricks. Proposals for closing that gap surfaced repeatedly over the past year, though none are usable today:

  • Brian Kardell proposed a switch() statement, which Tab Atkins built on.
  • Jonathan Neal proposed a media query variation for conditional values.
  • Lea Verou proposed "higher level custom properties."

The interest is easy to explain. Developers reach for state-driven UI often: a visual change after some scrolling, a change once a numeric input enters a range, a component with several states. Much of the popular JavaScript UI ecosystem exists precisely to build interfaces from state. Shifting that styling into CSS would reduce the JavaScript required and improve separation of concerns.

Custom properties are the natural foundation: change the property's value and a whole block of styles can follow as a side effect. Changing a class from JavaScript already achieves this, but it assumes you can or want to write that JavaScript. Custom properties can also be altered in other ways, such as media queries or HTML changes, which keeps business logic apart from visual style logic.

Pausing on a keyframe with animation

@keyframes can act as a switch. The animation property lets you select exactly which frame to display and pause on it, mimicking a switch-case or state-based styling. Three properties do the work:

  • animation-delay: negative delay values force a specific frame (or a point between frames) to take effect; positive values do not behave this way. This is the mechanism for forcing states.
  • animation-play-state: paused: nothing actually animates, so the animation stays paused.
  • animation-duration: its value is irrelevant beyond providing a time span for the keyframes, but it must be longer than the delay. Something like 100.001s lets a 100s delay still reach the last keyframe.

In the demonstration, a range input moves animation-delay between -100s and 0s.

Booleans and frame selection

Two caveats come with the trick. It only works with numeric values, since it performs math — colors and strings are out. And it needs a way to test conditions, which means building booleans.

Take a variable --value: 10 allowed to range from 0 to 100, and suppose color should apply above 5. clamp() makes the test possible: it confines a computed value to a range while declaring an ideal value, and that range is all a boolean variable needs.

--is-above-5: clamp(0, var(--value) - 5, 1)
--value--is-above-5Result
000 – 5 = -5, clamp() forces a value no less than 0
202 – 5 = -3, clamp() forces a value no less than 0
505 – 5 = 0
717 – 5 = 2, clamp() forces a value no larger than 1

Any expression in the second argument of clamp() will output 0 (or below) or 1 (or above), so avoid math that could land strictly between 0 and 1.

The range input's only role is to broadcast state: it sets --value, --min and --max, updating --value through an oninput event. That is the minimum needed for state-like behavior with no JavaScript.

The same variables can also yield a "completed" percentage for a progress bar through CSS math:

--completed: calc((var(--value) - var(--min) ) / (var(--max) - var(--min)) * 100);

That percentage produces another condition for changing styles by state:

--over-30: clamp(0, var(--completed) - 30, 1);
--over-70: clamp(0, var(--completed) - 70, 1);
/* ...and so on... */

To pick a specific keyframe, max() does the work:

--frame: max( 
    calc(1 - var(--over-30)), 
    var(--over-30) * 2, 
    var(--over-70) * 3, 
    var(--is-100)  * 4 
);

Each boolean with value 1 toggles a frame number, and because the function is max(), --frame resolves to the largest toggled frame. There are many formulas that reach the same goal; the practical aim is something short and readable.

The color change in the demo transitions slightly. Setting background: currentColor; on the fill area and inheriting from the parent would avoid JavaScript, but CSS Houdini demonstrates the alternative: declaring a variable's type grants it a transition.

The boolean trick is already common in heavily customized CSS-only components driven by many variables, and it opens use cases beyond those covered here.