/html-dom /Basic
GitHub 5008★

Get the position of an element relative to another

Assume that we're going to measure the top and left positions of the ele element relative to the target element.

// Get the top, left coordinates of two elements
const eleRect = ele.getBoundingClientRect();
const targetRect = ele.getBoundingClientRect();

// Calculate the top and left positions
const top = eleRect.top - targetRect.top;
const left = eleRect.left - targetRect.left;

See also

Follow me on and to get more useful contents.