I'm new to using CSS variables with calc() so forgive me if this is obvious, but I'm trying to perform the following calculation and can't for the life of me figure out why it's not working:
$h2-font-size: 21px;
--padding-top: calc(#{$h2-font-size}/3);
--padding-bottom: calc(#{$h2-font-size}/var(--padding-top));
padding-top: var(--padding-top);
padding-bottom: var(--padding-bottom);
The first part calculates OK (i.e. --padding-top: calc(#{$h2-font-size}/3);
) correctly works out to 7
and populates that OK in the padding-top:
value.
However, the padding-bottom:
value calculates as 0
.
Any idea where I'm going wrong?
Initially I wondered if it was because I was using a sass
variable, but if that was the problem I would have expected --padding-top:
to fail.... but it doesn't.
I notice that if I DON'T add a px value to the font-size, and place the font-size AFTER the CSS variable, then I do get a value returned (albeit, the wrong number for my needs):
--padding-bottom: calc(var(--padding-top)/21);
= 0.3333333
However, if I try to swap the order around to give me the right calculation, I get 0
:
--padding-bottom: calc(21/var(--padding-top));
I know I still have a lot to learn, but I'm hoping someone can pls help steer me in the right direction!
Thanks