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
orfixed
.
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.