2

In CSS, can we we multiply a variable by some integer like the code below ?

:root {
     --x: 1em;
}

.class2 {
     --x: calc(2em * var(--x));
}
Turnip
  • 35,836
  • 15
  • 89
  • 111
EEAH
  • 715
  • 4
  • 17

1 Answers1

2

A quick check on the MDN docs unfortunately did not shine light on this. So unless you're willing to dive into the spec, here's a quick test:

:root {
  --x: 4em;
}

.class2 {
  --x: calc(0.5 * var(--x));
  font-size: var(--x);
}
<div class="class2">
  Test - doesn't work as intended
</div>

By the looks of it not only does the calculcation not work - which is unfortunate by itself - but it even seems to invalidate the custom property for .class2.

Just to make sure the formula/approach of using other variables to create computed variables in general is valid:

:root {
  --x: 4em;
}

.class2 {
  --y: calc(0.5 * var(--x));
  font-size: var(--y);
}
<div class="class2">
  Test - <strike>doesn't</strike> works as intended
</div>
connexo
  • 53,704
  • 14
  • 91
  • 128
  • 2
    the first one is invalid because you cannot express a variable with itself, it creates a cycle: https://www.w3.org/TR/css-variables-1/#cycles – Temani Afif Jun 13 '20 at 09:57