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