Skip to content
Related Articles

Related Articles

HTML | DOM getBoundingClientRect() Method

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 10 Aug, 2022

HTML DOM getBoundingClientRect() Method returns the relative positioning to the viewport. It returns eight properties: left, top, right, bottom, x, y, width, height. Scrolling will change the position value. 


var rect = div.getBoundingClientRect();



<!DOCTYPE html>
        HTML | DOM getBoundingClientRect() Method
        function myFunction() {
            var div = document.getElementById("myDiv");
            var rectangle = div.getBoundingClientRect();
            x = rectangle.left;
            y =;
            w = rectangle.width;
            h = rectangle.height;
            alert("Left:" + x + ", Top:" + y +
                ", Width:" + w + ", Height:" + h);
    <button onclick="myFunction()">
    <div style="height:300px;
        <div id="myDiv" style="width:350px;
                               border:2px SOLID green;">
            Use scrollbar to change the position.
        <div style="width:1500px;
                    height:1500px; ">


Before Click: 


After Click:


Supported Browsers:

  • Chrome 2.0 and above
  • Edge 12 and above
  • Internet Explorer 4.0 and above
  • Firefox 3.0 and above
  • Opera 9.5 and above
  • Safari 4.0 and above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!