Andreas
Andreas

Reputation: 99

How to: Subtract a CSS value from a JS variable height?

How can I subtract from the navbar_height for example 2rem in the following function?

navbar_height = document.querySelector(".navbar").offsetHeight;
document.body.style.paddingTop = navbar_height + "px";

Thanks!

Upvotes: 0

Views: 74

Answers (2)

tacoshy
tacoshy

Reputation: 13012

You can use parseFloat(getComputedStyle(document.documentElement).fontSize) to get the rem value:

let rem = parseFloat(getComputedStyle(document.documentElement).fontSize);
console.log(rem);

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72937

You can use calc

navbar_height = document.querySelector(".navbar").offsetHeight;
document.body.style.paddingTop = `calc(${navbar_height}px - 2rem)`;

Upvotes: 1

Related Questions