Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Insert Javascript variable in CSS calc() function

I've got a JS variable representing a width in pixels that I'd like to use inside a CSS calc() function.

The variable is holding the width of a div's scrollbar.

let table = document.getElementById("RGtable");

let offsetWidth = RGtable.offsetWidth;
let clientWidth = RGtable.clientWidth;

function getScrollbarWidth() {
  return offsetWidth - clientWidth;
};

let scrollbarWidth = getScrollbarWidth() + "px";

let header = document.getElementById("tableHeader");

// This is where I have the problem
header.style.setProperty('width', 'calc(100% - scrollbarWidth)');

I've tried:

header.style.setProperty('width', 'calc(100% - 'getScrollbarWidth() + 'px'')');

which gives me errors, and

header.style.setProperty('width', 'calc(100% -  ${width})')

which is based off of this post.

I know I'm missing something, just can't figure out what!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to properly interpolate the string:

with backticks (not single quotes)

header.style.setProperty('width', `calc(100% - ${scrollbarWidth})`);

or with string concatenation

header.style.setProperty('width', 'calc(100% - ' + scrollbarWidth + ')');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!