Retrieve the position (X,Y) of an HTML element

Ask a question+
0

I want to know how to get the X and Y position of HTML elements such as img and div in JavaScript.

add comment

1 Answer

0

The correct approach is to use element.getBoundingClientRect():

var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);
add comment

Your Answer