offsetLeft property
The offsetLeft property contains
the element's left offset relative to
offsetParent.
It contains the distance from
offsetParent to the
element border.
Syntax
element.offsetLeft;
Example
Let's find the offset of an element
relative to its own offsetParent:
<div id="parent" style="position: relative;">
<div id="elem" style="position: absolute; left: 100px"></div>
</div>
let elem = document.querySelector('#elem');
console.log(elem.offsetLeft);
The code execution result:
100
Example
Margins
are also included into offset:
<div id="parent" style="position: relative;">
<div id="elem" style="position: absolute; left: 100px; margin: 50px"></div>
</div>
let elem = document.querySelector('#elem');
console.log(elem.offsetLeft);
The code execution result:
150
See also
-
the
offsetTopproperty
that contains the top offset of an element -
the
getBoundingClientRectmethod
that finds the offset of an element