Code has been added to clipboard!

Use jQuery .offsetParent() to Find the Closest Positioned Parent

Reading time 1 min
Published Jan 22, 2018
Updated Jan 21, 2020

jQuery offsetParent: Main Tips

  • jQuery .offsetParent() returns the first positioned parent.
  • Positioned refers to an element that has the CSS position property with one of the following values: relative, absolute or fixed.

Using .offsetParent()

The .offsetParent() jQuery method looks through the DOM tree of specified elements and finds the ancestors. Then the offset parents function generates a new jQuery object with the closest positioned parent.

Hover over the element in the example to see its positioned offset parent change color:

Example
$("div").hover(() => {
    $("div").offsetParent().css("color", "#41914e");
});

Follow this syntax for the jQuery .offsetParent() method:

$(selector).offsetParent();

Tip: you can return the element position relative to the offset parent by applying the jQuery .position() method.

Latest Coupon Found:

Verified STAFF PICK

TOP-RATED CODING COURSES

Learn the skills of tomorrow

The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!

Expiration date: 18/04/2021
2287 People Used
Only 92 Left
Rating
5.0