Sass Variable in CSS calc() function
To use $variables
inside your calc()
of the height property:
HTML:
<div></div>
SCSS:
$a: 4em;
div {
height: calc(#{$a} + 7px);
background: #e53b2c;
}
Interpolate:
body
height: calc(100% - #{$body_padding})
For this case, border-box would also suffice:
body
box-sizing: border-box
height: 100%
padding-top: $body_padding
Even though its not directly related. But I found that the CALC code won't work if you do not put spaces properly.
So this did not work for me calc(#{$a}+7px)
But this worked calc(#{$a} + 7px)
Took me sometime to figure this out.
I have tried this then i fixed my issue. It will calculate all media-breakpoint automatically by given rate (base-size/rate-size)
$base-size: 16;
$rate-size-xl: 24;
// set default size for all cases;
:root {
--size: #{$base-size};
}
// if it's smaller then LG it will set size rate to 16/16;
// example: if size set to 14px, it will be 14px * 16 / 16 = 14px
@include media-breakpoint-down(lg) {
:root {
--size: #{$base-size};
}
}
// if it is bigger then XL it will set size rate to 24/16;
// example: if size set to 14px, it will be 14px * 24 / 16 = 21px
@include media-breakpoint-up(xl) {
:root {
--size: #{$rate-size-xl};
}
}
@function size($px) {
@return calc(#{$px} / $base-size * var(--size));
}
div {
font-size: size(14px);
width: size(150px);
}