jQuery .offsetParent() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.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
CallWhat it does
Matched setElement(s) to start from
ArgumentsMethod takes no parameters
Stop conditionAncestor with non-static position
Return valueNew jQuery set of positioned ancestor(s)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element whose positioning context you need.
Step 2Call .offsetParent() — no arguments.
Step 3Static ancestors are skipped; relative / absolute / fixed stop the walk.

Return Value

Return ValueDescription
jQuery objectMost 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).

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.

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.

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.

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: .contents() Method

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

.contents() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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