Nathan Osman
Nathan Osman

Reputation: 73195

How to find the position of an element within its parent container?

Consider the following diagram:

enter image description here

I would like to calculate the vertical offset of the child element within its immediate parent. Note that the child element has a margin (indicated by the lighter blue).

jQuery provides .position(), but unfortunately the method calculates offset:

"...relative to the offset parent."

The parent element in this case does not have position set to relative and unfortunately this cannot be changed.

In spite of this, is there any way to get the child element's offset relative to its immediate parent?

Upvotes: 2

Views: 252

Answers (1)

Jon
Jon

Reputation: 437376

You can do that with

$(this).offset().top - $(this).parent().offset().top

Upvotes: 2

Related Questions