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