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

172
Views
Can `getBoundingClientRect` return values in `rem`?

I am using rems for my CSS sizes and am also using getBoundingClientRect in javascript. getBoundingClientRect returns it's values in px by default. Is it possible to get it to return the values in rems? It would make my code a little simpler.

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

0

You will have to convert it by dividing the pixels by the computed font size of the document:

function convertPixelsToRem(px) {    
    return px / parseFloat(getComputedStyle(document.documentElement).fontSize.replace('px', ''));
}

console.log(convertPixelsToRem(div.getBoundingClientRect().width))
#div{
  width:100px;
  height:100px;
  background-color:grey;
  margin-bottom:10px;
}

#div2{
  width:6.25rem;
  height:100px;
  background-color:grey;
}
<div id="div"></div>
<div id="div2"></div>

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!