JavaScript getBoundingClientRect() vs offsetHeight while calculate element height

What is the best way to get element height:

var myElement = document.querySelector('.some-class');

var height = myElement.getBoundingClientRect().height;


var myElement = document.querySelector('.some-class');

var height = myElement.offsetHeight;  

Most of the time these are the same as width and height of getBoundingClientRect(), when there aren’t any transforms applied to the element. In case of transforms, the offsetWidth and offsetHeight returns the element’s layout width and height, while getBoundingClientRect() returns the rendering width and height. As an example, if the element has width: 100px; and transform: scale(0.5); the getBoundingClientRect() will return 50 as the width, while offsetWidth will return 100.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Does ES6 import/export need ".js" extension?

Similar Posts