jQuery .offsetParent() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .offsetParent()?

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

Remember
$("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
NeedUse
Closest positioned ancestor$el.offsetParent()
Immediate parent.parent()
Ancestor by selector.closest(sel)
Document coordinates.offset()
Position relative to offset parent.position()

How to Use .offsetParent()

Start from a child, walk up until a positioned ancestor appears.

  1. Select the element whose positioning context you need.
  2. Call .offsetParent() — no arguments.
  3. Static ancestors are skipped; relative / absolute / fixed stop the walk.
  4. If none exist, browsers typically return html or body.
  5. Use the result with .offset() / .position() for layout math.
PieceRequired?Role
Matched setYesElement(s) to start from
ArgumentsNoneMethod takes no parameters
Stop condition—Ancestor with non-static position
Return value—New jQuery set of positioned ancestor(s)

Pseudocode

Pseudocode
call $(starts).offsetParent()

result = empty set
for each start in the matched set:
    ancestor = start.parentElement
    while ancestor is not null:
        if ancestor.position is relative
           or absolute
           or fixed:
            add ancestor to result
            break
        ancestor = ancestor.parentElement
    // if none found → document root (html/body)

return result as a new jQuery object

Cheat Sheet

Keep ancestor methods side by side.

TopicDetail
Syntax.offsetParent()
SincejQuery 1.2.6
Looks forrelative / absolute / fixed
Skipsposition: static
Includes self?No — ancestors only
vs .parent()Immediate parent vs positioned ancestor
vs .closest()Your selector vs position rule
DOM change?No — query only

Live Preview

From li.item-a, compare .offsetParent(), .parent(), and .closest("li").

  • I
  • II
    • A
    • B
      • 1
      • 2
    • C
  • III

Sample sets li.item-ii to position: relative via jQuery (official demo idea).

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official: From Item A to Positioned II

Mark the offset parent of nested item A (jQuery API demo).

jQuery
$("li.item-ii").css("position", "relative");
$("li.item-a").offsetParent().append(" [op]");
Try It Yourself

How It Works

  1. Position. Set li.item-ii to relative.
  2. Start. Select li.item-a.
  3. Walk. Skip the static ul parent.
  4. Stop. Land on positioned II and mark it.

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);
Try It Yourself

How It Works

  1. parent. Returns the direct wrapper (#wrap).
  2. offsetParent. Skips static #wrap.
  3. Find. Lands on positioned #box.
  4. Lesson. Use .offsetParent() when you need the positioning context.

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
);
Try It Yourself

How It Works

  1. closest. li.item-a matches "li" → returns itself.
  2. offsetParent. Ignores self; searches ancestors only.
  3. Result. Positioned II is the offset parent.
  4. Pick. Use .closest() for selector matches; .offsetParent() for position context.

Example 4 — Absolute Child Inside Relative Parent

Confirm the relative container is the offset parent of an absolute child.

jQuery
$("#stage").css("position", "relative");
$("#pin").css("position", "absolute");

$("#out").text(
  "offsetParent id: " + $("#pin").offsetParent().attr("id")
);
Try It Yourself

How It Works

  1. Stage. Make #stage relative.
  2. Pin. Make #pin absolute.
  3. Query. #pin.offsetParent() returns #stage.
  4. Use. That ancestor is the reference for .position().

Example 5 — No Positioned Ancestor

When every ancestor is static, the result is typically the document root.

jQuery
var tag = $("#lonely").offsetParent().prop("tagName");

$("#out").text("offsetParent tag: " + tag);
Try It Yourself

How It Works

  1. All static. No relative/absolute/fixed ancestor exists.
  2. Fallback. The browser returns body or html.
  3. Check. Read tagName to see which root you got.
  4. Tip. Add an explicit positioned wrapper when you need a stable context.

Edge Cases & Pitfalls

Why the offset parent is not always the element you expect.

static

Static ancestors are skipped

Default CSS position: static never becomes an offset parent.

self

Never returns the start element

Even if the start node is positioned, the method looks at ancestors only.

root

No positioned ancestor → document root

Expect body or html depending on the browser when the tree is all static.

sticky

sticky is not in the official list

Docs specify relative, absolute, and fixed. Do not assume sticky always counts.

Browser Support

.offsetParent() has been part of jQuery since 1.2.6. It works in every browser that runs your loaded jQuery version and mirrors the idea of the native DOM offsetParent property.

✓ Stable · jQuery 1.2.6+

jQuery .offsetParent()

Supported across jQuery 1.x (1.2.6+), 2.x, and 3.x. Prefer a current jQuery 3.x build. Result depends on live CSS position values.

100% With jQuery
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.offsetParent() Universal

Bottom line: Safe for layout and animation helpers. Ensure a positioned ancestor exists when you need a predictable context.

Performance Notes

ApproachCostBest for
.parent()CheapestOne step up
.offsetParent()Walk until positionedPositioning context
.closest(sel)Walk until selectorKnown ancestor selector

Cost grows with how deep the walk is. Prefer an explicit positioned wrapper near the element when you call this often (for example in animation loops).

Key Takeaways

  • Role: closest ancestor with relative / absolute / fixed.
  • Skips: position: static (the default).
  • vs parent: immediate parent ≠ positioning context.
  • Pairs with: .offset() and .position().

One line: .offsetParent() finds the nearest positioned ancestor — the context for offsets and absolute placement.

Frequently Asked Questions

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

Next: .parent() Method

Get the immediate parent of each element in the matched set.

.parent() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful