.position()

Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

.position()🡢 Object

The .position() method allows us to retrieve the current position of an element (specifically its margin box) relative to the offset parent (specifically its padding box, which excludes margins and borders). Contrast this with .offset(), which retrieves the current position relative to the document. When positioning a new element near another one and within the same containing DOM element, .position() is the more useful.

Returns an object containing the properties top and left.

Note: jQuery does not support getting the position coordinates of hidden elements or accounting for margins set on the <html> document element.

Access the position of the second paragraph:

JS
<div>
  <p>Hello</p>
</div>
<p></p>
CSS
div {
  padding: 15px;
}
p {
  margin-left: 10px;
}
HTML
var p = $("p").first();
var position = p.position();
$("p")
  .last()
  .text("left: " + position.left + ", top: " + position.top);
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)