fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← JavaScript

JavaScript / 2 MIN READ

getBoundingClientRect()

Get element positions relative to viewport

From the original Fervor library. Examples may use older package versions.

Using the getBoundingClientRect() Method in JavaScript

In this tutorial, we’ll explore the getBoundingClientRect() method in JavaScript, which is available on all DOM elements. This method provides valuable information about an element’s position relative to the viewport. We’ll dive into the details of how to use this method effectively.

Introduction to getBoundingClientRect()

The getBoundingClientRect() method is a powerful tool for obtaining position and size information about a DOM element within the viewport. By calling this method on an element, you can retrieve an object that contains various properties detailing its position and dimensions.

Properties Returned by getBoundingClientRect()

The returned object from getBoundingClientRect() includes the following properties:

  • x (or left): The distance between the left edge of the viewport and the left edge of the element.
  • y (or top): The distance between the top edge of the viewport and the top edge of the element.
  • width: The width of the element (including padding but excluding borders or margins).
  • height: The height of the element (including padding but excluding borders or margins).
  • right: The distance between the left edge of the viewport and the right edge of the element.
  • bottom: The distance between the top edge of the viewport and the bottom edge of the element.

Using getBoundingClientRect() in JavaScript

Let’s look at an example of how you can utilize the getBoundingClientRect() method to retrieve an element’s position relative to the viewport:

<style>
#myDiv {
    width: 100px;
    height: 100px;
    background-color: red;
}
</style>
<div id="myDiv"></div>

<script>
// Get a reference to the div element
const myDiv = document.getElementById("myDiv");

// Get the position of the div relative to the viewport
const rect = myDiv.getBoundingClientRect();

// Log the position to the console
console.log(rect.x, rect.y, rect.width, rect.height, rect.right, rect.bottom);
</script>

In this example, we start by obtaining a reference to the div element using document.getElementById(). We then call getBoundingClientRect() on the div element to acquire its position and dimensions. Finally, we log the retrieved values to the console for observation.

Notes on getBoundingClientRect()

It’s important to note that the getBoundingClientRect() method returns a DOMRect object, which behaves similarly to a plain JavaScript object. You can access its properties using dot notation, just like any other object in JavaScript.


By using the getBoundingClientRect() method, you can dynamically determine the position and size of DOM elements relative to the viewport. This information can be crucial for various interactive and responsive web applications.

Home


Keep your curiosity going.Explore more JavaScript →
287 TUTORIALS · 22 TOPICSREADY