.offsetParent() (since jQuery 1.2.6) returns the closest positioned ancestor of each matched element. It takes no arguments. Official docs note this is useful for calculating offsets, animations, and placing objects on the page.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("li.item-a").offsetParent(); // official: positioned "II"
$("#box").offsetParent(); // nearest relative/absolute/fixed
$(".child").offsetParent().css(…); // style the positioning context
// parent() → immediate parent (any position)
// closest(sel) → nearest match to your selector
// offsetParent() → nearest positioned ancestor
Call
What it does
Matched set
Element(s) to start from
Arguments
Method takes no parameters
Stop condition
Ancestor with non-static position
Return value
New jQuery set of positioned ancestor(s)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element whose positioning context you need.
Step 2
Call .offsetParent() — no arguments.
Step 3
Static ancestors are skipped; relative / absolute / fixed stop the walk.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Official: From Item A to Positioned II
Mark the offset parent of nested item A (jQuery API demo).
II [op]
(item A’s offset parent is the positioned II)
How It Works
Position. Set li.item-ii to relative.
Start. Select li.item-a.
Walk. Skip the static ul parent.
Example 2 — .offsetParent() vs .parent()
Immediate parent is not always the positioning context.
jQuery
$("#box").css("position", "relative");
var p = $("#child").parent()[0].id;
var op = $("#child").offsetParent()[0].id;
$("#out").text("parent=" + p + " · offsetParent=" + op);
parent=wrap · offsetParent=box
wrap is static; box is relative
How It Works
parent. Returns the direct wrapper (#wrap).
offsetParent. Skips static #wrap.
Find. Lands on positioned #box.
Example 3 — .offsetParent() vs .closest("li")
.closest() can match the start element; .offsetParent() never does.
jQuery
$("li.item-ii").css("position", "relative");
var c = $("li.item-a").closest("li").hasClass("item-a");
var op = $("li.item-a").offsetParent().hasClass("item-ii");
$("#out").text(
"closest is item-a? " + c +
" · offsetParent is item-ii? " + op
);
.offsetParent() walks up from each element in the current set and returns the closest ancestor whose CSS position is relative, absolute, or fixed. Useful for understanding the positioning context used for offsets and animations.
An element with position: relative, absolute, or fixed. Static (the CSS default) ancestors are skipped. Official jQuery docs define positioned this way.
.parent() returns the immediate parent only. .offsetParent() may skip several static ancestors until it finds a positioned one — or the document root when none exist.
.closest(selector) finds the nearest ancestor (or self) matching a selector you provide. .offsetParent() finds the nearest positioned ancestor — no selector needed.
No. It searches ancestors. Even if the starting element is positioned, the method looks upward for a positioned ancestor.
No. It only returns a new jQuery object wrapping the positioned ancestor(s).