jQuery .offset() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

.offset() gets or sets an element’s document-relative coordinates as { top, left } (getter since 1.2; setter since 1.4). It measures the border box — margins are excluded.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("p").offset()                 // { top: Number, left: Number }
$("p").offset({ top: 10, left: 30 })
$("p").offset(function (i, coords) { ... })

.offset()    → relative to the document
.position()  → relative to the offset parent
CallWhat it does
.offset()Learn jQuery .offset(): get or set document-relative top and left coordinates, compare with .position(), setter since 1.4, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select a visible DOM element.
Step 2Call .offset() with no args to read { top, left }.
Step 3Pass { top, left } to move the element in the document (jQuery 1.4+).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Get Document Coordinates

Official style: read { top, left } for an element.

jQuery
var offset = $("p").last().offset();

console.log("left: " + offset.left + ", top: " + offset.top);
Try It Yourself

How It Works

  1. Select. Target the last paragraph with $("p").last().
  2. Read. .offset() returns a plain object with top and left.
  3. Meaning. Those numbers are distances from the document origin.

Example 2 — Set Document Coordinates

Pass { top, left } to move an element (since 1.4).

jQuery
$("p").last().offset({ top: 10, left: 30 });

var o = $("p").last().offset();
console.log("left: " + o.left + ", top: " + o.top);
Try It Yourself

How It Works

  1. Setter. Pass an object with numeric top and left.
  2. Static → relative. If the element was static, jQuery switches it to relative.
  3. Border box. The new position is the border box relative to the document.

Example 3 — .offset() vs .position()

Same element, two coordinate systems.

jQuery
var $box = $("#box");
var o = $box.offset();
var p = $box.position();

console.log("offset:   left " + o.left + ", top " + o.top);
console.log("position: left " + p.left + ", top " + p.top);
Try It Yourself

How It Works

  1. Document. .offset() measures from the page origin.
  2. Parent. .position() measures from the offset parent.
  3. Why both? Drag overlays often need document coords; nested layout needs parent coords.

Frequently Asked Questions

.offset() with no arguments returns { top, left } for the first matched element relative to the document. .offset({ top, left }) or .offset(function) sets those document coordinates on every matched element and returns the jQuery object for chaining.
.offset() is relative to the document. .position() is relative to the offset parent (usually the nearest positioned ancestor). Use .offset() for page-level placement; use .position() inside a positioned container.
When you set .offset(), jQuery changes static elements to position: relative so top/left can take effect. Prefer setting position yourself if you need long-term CSS control.
The getter has been available since jQuery 1.2. The setter .offset(coordinates) and the callback form arrived in jQuery 1.4.
No. display:none elements are not laid out, so coordinates are unreliable. visibility:hidden can still be measured. Measure while the element is rendered.
No. Values can be fractional on sub-pixel layouts or zoomed pages. Round only when your UI needs integers.

Next: .position() Method — Offset Parent Coordinates

Learn coordinates relative to the offset parent.

.position() Method — Offset Parent Coordinates 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.

9 people found this page helpful