# The --var: ; hack to toggle multiple values with one custom property

DevFeed: [The --var: ; hack to toggle multiple values with one custom property](<https://devfeed.tech/articles/the-var-hack-to-toggle-multiple-values-with-one-custom-property-52124.md>)

Original publisher: [Read original article](<https://lea.verou.me/2020/10/the-var-space-hack-to-toggle-multiple-values-with-one-custom-property/>)

Author: Lea Verou

Published: 2020-10-12T00:00:00Z

Content type: article

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [Web Development](<https://devfeed.tech/topics/web-development.md>), [User interface design](<https://devfeed.tech/topics/ui-design.md>), [if statement](<https://devfeed.tech/topics/if-statement.md>), [concatenation](<https://devfeed.tech/topics/concatenation.md>)

Tags: [all](<https://devfeed.tech/tags/all.md>), [allowed](<https://devfeed.tech/tags/allowed.md>), [apply](<https://devfeed.tech/tags/apply.md>), [background](<https://devfeed.tech/tags/background.md>), [blog](<https://devfeed.tech/tags/blog.md>), [border](<https://devfeed.tech/tags/border.md>), [box](<https://devfeed.tech/tags/box.md>), [but](<https://devfeed.tech/tags/but.md>), [button](<https://devfeed.tech/tags/button.md>), [call](<https://devfeed.tech/tags/call.md>), [can](<https://devfeed.tech/tags/can.md>), [code](<https://devfeed.tech/tags/code.md>), [color](<https://devfeed.tech/tags/color.md>), [css](<https://devfeed.tech/tags/css.md>), [css-variables](<https://devfeed.tech/tags/css-variables.md>), [custom](<https://devfeed.tech/tags/custom.md>), [hack](<https://devfeed.tech/tags/hack.md>), [multiple](<https://devfeed.tech/tags/multiple.md>), [original](<https://devfeed.tech/tags/original.md>), [property](<https://devfeed.tech/tags/property.md>), [tips](<https://devfeed.tech/tags/tips.md>), [toggle](<https://devfeed.tech/tags/toggle.md>), [values](<https://devfeed.tech/tags/values.md>), [var](<https://devfeed.tech/tags/var.md>), [with](<https://devfeed.tech/tags/with.md>)

## AI overview

The excerpt describes a CSS custom-property toggle: set a variable used in `var()` fallbacks to `initial` to activate the fallback values, or to whitespace (`--foo: ;`) to leave surrounding declarations unchanged. This can append or enable values across properties and rules using one variable, such as styling a raised button. The technique only supports appending values or choosing a whole value versus `initial`; it cannot express arbitrary alternatives such as red versus white backgrounds. The author notes readability concerns and suggests comments or named constants like `--ON` and `--OFF`. A proper CSS `if()` is described as a future alternative under discussion.

## Source excerpt

What if I told you you could use a single property value to turn multiple different values on and off across multiple different properties and even across multiple CSS rules? What if I told you you could turn this flat button into a glossy skeuomorphic button by just tweaking one custom property --is-raised, and that would set its border, background image, box and text shadows in one fell swoop? How, you may ask? The crux of this technique is this: There are two custom property values that work almost everywhere there is a var() call with a fallback. The more obvious one that you probably already know is the initial value, which makes the property just apply its fallback. So, in the following code: background: var(--foo, linear-gradient(white, transparent)) hsl(220 10% 50%); border: 1px solid var(--foo, rgb(0 0 0 / .1)); color: rgb(0 0 0 var(--foo, / .8)); We can set --foo to initial to enable these "fallbacks" and append these values to the property value, adding a gradient, setting a border-color, and making the text color translucent in one go. But what to do when we want to turn these values off? Any non-initial value for --foo (that doesn't create cycles) should work. But is there one that works in all three declarations? It turns out there is another value that works everywhere, in every property a var() reference is present, and you'd likely never guess what it is (unless you have watched any of my CSS variable talks and have a good memory for passing mentions of things). Intrigued? It's whitespace! Whitespace is significant in a custom property. When you write something like this: --foo: ; This is not an invalid declaration. This is a declaration where the value of --foo is literally one space character. However, whitespace is valid in every CSS property value, everywhere a var() is allowed, and does not affect its computed value in any way. So, we can just set our property to one space (or even a comment) and not affect any other value present in the declar