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

javascriptelement.offsetTop;

Example

Let's find the offset of an element relative to its own offsetParent:

html<div id="parent" style="position: relative;">
  <div id="elem" style="position: absolute; top: 100px;"></div>
</div>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.offsetTop);

The code execution result:

javascript100

Example

Margins are also included into offset:

html<div id="parent" style="position: relative;">
  <div id="elem" style="position: absolute; top: 100px; margin: 50px;"></div>
</div>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.offsetTop);

The code execution result:

javascript150

See also