rem px in Javascript

Malus Jan picture Malus Jan · Apr 10, 2016 · Viewed 29.7k times · Source

I use this code in JS:

bigPhoto.setAttribute("width", "200rem");

The result is in pixel in html source,

enter image description here

But:

enter image description here

When I change it to 20rem the photo become really small.

I couldn't solve the problem which is mentioned in the photos.

Answer

etham picture etham · Mar 13, 2017

Another method would be to convert rem into pixels:

function convertRemToPixels(rem) {    
    return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
}

This can be useful when you must perform some arithmetic in js (such as using the position of the mouse to display a tooltip...)