Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)
edited by

Suppose that I have a <div> that I wish to center in the browser's display (viewport). To do so, I need to calculate the width and height of the <div> element.

What should I use? Please include information on browser compatibility.

1 Answer

0 votes
by (40.7k points)

Try using the .offsetWidth and .offsetHeight properties. 

var width = document.getElementById('foo').offsetWidth;

Note: They belong to the element, not to the .style.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 9, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer

Browse Categories

...