I am using SCSS and wanted to specify a variable at top that is based on a sum of a px and a rem value.
I understand that it is not possible to combine unit types in SCSS variables, so instead I use calc()
:
$navbar-top: calc(40px + 1 rem);
Further down in the style sheet, I wanted to include a negated version of $navbar-top
.
I tried both these, with no success:
(-$navbar-top)
-#{$navbar-top}
Any idea how I can negate the SCSS variable, without having to declare a new variable, or writing calc again throughout the CSS.
Thanks.
EDIT
- As noted in the comments below, this is not a duplicate of Sass negative variable value?
- This question is about negating a variable set with calc(), which is unrelated to the above SO post. I have noted both solutions presented in that question, above. None of them work in this particular case.
EDIT 2: SCSS SOLUTION BASED ON TEMANI'S ANSWER BELOW
Declare the necessary variables at top, including a "negator" (-1):
$navbar-top: calc(40px + 1rem);
$neg: -1;
Then, the variable can be used throughout (negated or not), like this:
/* Non-negated */
margin: $navbar-top 0 0;
/* Negated */
margin: calc(#{$neg} * #{$navbar-top}) 0 0;
The negated version above will compile to the following CSS:
margin: calc(-1 * calc(40px + 1rem)) 0 0;