JavaScript element.getBoundingClientRect() Method
You are Here:
JavaScript element.getBoundingClientRect() Method
The element.getBoundingClientRect()
method returns the size of an element and its position relative to the viewport.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<div id="outerDiv">
<div id="innerDiv">Scroll me right and left</div>
</div>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
var elem = document.getElementById("outerDiv");
function myFunction(){
var rect = elem.getBoundingClientRect();
var txt = "";
txt += "<br> Left : " + rect.left;
txt += "<br> Top : " + rect.top;
txt += "<br> Width : " + rect.width;
txt += "<br> Height : " + rect.height;
x.innerHTML += txt;
}
</script>
</body>
</html>
Syntax
element.getBoundingClientRect()
Return Values
Value | Explanation |
---|---|
Object | Returns the size of an element and its position relative to the viewport. |
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.