Sometimes, you need to know the width and height of your element. You may also need to know where the element is located in the document.
Getting this information is easy with getBoundingClientRect.
const domRect = element.getBoundingClientRect()domRect is an object that contains 6 values:
topβ the distance from the top of the document to the top of the elementbottomβ the distance from the top of the document to the bottom of the elementleftβ the distance from the left of the document to the left of the elementrightβ the distance from the left of the document to the right of the elementwidthβ the width of the elementheightβ the height of the element
There are two more values, x and y, but they mean the same thing as left and top.
Exercise
Go to any web page, open up your console and get use getBoundingClientRect to get at the DomRect of at least one element. You should see eight values.
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson