Conditional logic with CSS – The CSS custom property trick.

The whole world of conditional logic and bulk toggling in CSS only exists because of one tiny footnote in the CSS spec.

That line is:

Note: While must represent at least one token, that one token may be whitespace.

That means –foo: ; is valid.


This content originally appeared on DEV Community and was authored by Siddharth

The whole world of conditional logic and bulk toggling in CSS only exists because of one tiny footnote in the CSS spec.

That line is:

Note: While must represent at least one token, that one token may be whitespace.

That means --foo: ; is valid.

Now, this doesn’t read as some massive revelation that unlocks huge doors, but it is! Now we have the capability to make conditional statements all in CSS!

Here's a demo:

(Open the pen in a new tab and resize to see the full effect)

Let me explain:

  • There is a breakpoint setup here at 350px. This is where the variable --color changes from initial to an empty space
  • When the browser window is wider than 350px, the value of --color is initial
    • That makes the variable --color-when-small contain two values: initial and red which is invalid
    • So when we actually set the color and call that variable like background-color: var(--color-when-small, var(--color-when-big));, the second value (the fallback) is used because the first is invalid.
  • When the browser window is narrower than 350px, the value of --color is a space.
    • That makes the variable --color-when-small contain the value "(space)red", which is valid
    • So when we actually set the color and call that variable like background-color: var(--color-when-small, var(--color-when-big));, the first value is used

So, now we can flip the color between two values by changing a placeholder variable. I hope that clicks for you.

When we see this as simply changing a single value, it’s almost like ok, you’ve found a really complex way to change some padding, but you could have just changed the padding in the media query. But the trick is that now we have this placeholder variable that has changed and we can key into that to change unlimited other values.

We could have a single media query (or set of media queries) in our CSS that only toggles these placeholder variables and we use elsewhere to toggle values. That could be nice and clean compared to sprinkling media queries all over the CSS. It’s a proper toggle in CSS, like a form of IF/THEN logic that we haven’t quite had before.

For example, imagine we had a border for our earlier coloured div. Instead of changing the border directly in the media query, we can use the custom property again to change the border.

Here is an example:

(Open the pen in a new tab and resize to see the full effect)

If we were creating a complex card, this could come in handy.

This is not just for media queries. CSS trickery master Lea Verou has set her sights on this too:

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?

It’s the same trick!

This opens up a lot of possibilities in CSS. We could do AND, OR, XOR, NOR etc. with this trick. But I'm leaving that for you to find.

Thanks for reading!


This content originally appeared on DEV Community and was authored by Siddharth


Print Share Comment Cite Upload Translate Updates
APA

Siddharth | Sciencx (2021-04-11T08:31:25+00:00) Conditional logic with CSS – The CSS custom property trick.. Retrieved from https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/

MLA
" » Conditional logic with CSS – The CSS custom property trick.." Siddharth | Sciencx - Sunday April 11, 2021, https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/
HARVARD
Siddharth | Sciencx Sunday April 11, 2021 » Conditional logic with CSS – The CSS custom property trick.., viewed ,<https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/>
VANCOUVER
Siddharth | Sciencx - » Conditional logic with CSS – The CSS custom property trick.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/
CHICAGO
" » Conditional logic with CSS – The CSS custom property trick.." Siddharth | Sciencx - Accessed . https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/
IEEE
" » Conditional logic with CSS – The CSS custom property trick.." Siddharth | Sciencx [Online]. Available: https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/. [Accessed: ]
rf:citation
» Conditional logic with CSS – The CSS custom property trick. | Siddharth | Sciencx | https://www.scien.cx/2021/04/11/conditional-logic-with-css-the-css-custom-property-trick/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.