The spec footnote that enables it

The trick hinges on a line in the CSS Custom Properties specification that is easy to skim past: while <declaration-value> must represent at least one token, that token may be whitespace. In practice, this means --foo: ; is a valid declaration. James0x57 is credited with spotting the implications of that statement — branching conditional logic and bulk feature toggling built from custom properties alone.

Toggling a value with a placeholder variable

The mechanism is easiest to follow in a media-query toggle. Below a 900px max-width query, a variable such as --mq-sm changes from initial to an empty space. That switch propagates in two directions:

  • Above 900px, --mq-sm is initial, so --padding-when-small holds two values, initial and 2rem, which renders the variable invalid.
  • At or below 900px, --mq-sm is a space, making the value of --padding-when-small a space followed by 2rem, which is valid.

The consumer then reads padding: var(--padding-when-small, var(--padding-when-large)). When the first value is invalid, the fallback — the second argument — is used; when it is valid, the first value wins. That flips the padding between two values through a single placeholder variable.

Why the indirection matters

On its own, the demo looks like an elaborate way to change padding that could have been set directly inside the media query. The point is what the placeholder variable unlocks. Once one variable flips, it can be used to drive any number of other values. A single media query, or a small set of them, can toggle only those placeholders, and the rest of the stylesheet keys into them. Instead of scattering media queries throughout the CSS, you get a proper toggle — a form of IF/THEN logic that CSS previously lacked. James0x57 extended the idea to the logical operations AND, OR, XOR, NAND, NOR and XNOR and published related work in aug-attr-spliced.js.

The same trick, applied more broadly

Lea Verou later applied the whitespace-in-custom-properties behavior to a concrete case: a single property value that switches multiple values on and off across multiple properties and even multiple CSS rules. Her example uses it to produce variations on a button and to control four properties rather than one, which illustrates the concept more clearly than a single-property demo.

She also names the trade-offs. There is no way to express that the background should be red when --foo is set and white otherwise; some conditionals can be emulated by clever appending, but most cannot. Readability suffers as well, since --foo: ; looks like a mistake and --foo: initial looks strange to anyone unfamiliar with the technique.

Patrick Brosset's writing on lesser-known custom-property behavior covers adjacent wrinkles. Fallbacks can be nested to arbitrary depth:

color: var(--foo, var(--bar, var(--baz, var(--are, var(--you, var(--crazy)))));

And valid values may contain commas:

content: var(--foo, one, two, three);

In the second case the entire expression is one fallback whose value is one, two, three.

Where custom properties stand

These capabilities mark a shift in how custom properties get used. They began as a substitute for preprocessor variables, then became a tool for cascade and fallback behavior, then were increasingly paired with JavaScript. This toggle technique is the next step. Preprocessor variables have also kept a following, less for what only they can do and more for operations such as manipulating color values.