jQuery .offset() Method

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

What Is .offset()?

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

Remember
$("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
NeedUse
Page / document coords$el.offset()
Coords inside a positioned parent$el.position()
Scroll position.scrollTop() / .scrollLeft()

How to Use .offset()

Read or write document coordinates with a plain { top, left } object.

  1. Select a visible DOM element.
  2. Call .offset() with no args to read { top, left }.
  3. Pass { top, left } to move the element in the document (jQuery 1.4+).
  4. Use the callback form when each element needs different coordinates.
PieceRequired?Role
jQuery selectionYesElement(s) to measure or move
coordinates / functionSetter only (1.4+)New { top, left } (or per-element fn)
Return—Getter → object; setter → jQuery (chainable)

Pseudocode

Pseudocode
function offset(selection, value):
    if value is missing:
        el = first matched element
        return { top, left } relative to document

    for each el in selection:
        if value is a function:
            coords = value(index, currentOffset)
        else:
            coords = value
        if el.position is static:
            set position to relative
        move el so border-box = coords
    return selection

Cheat Sheet

Keep these facts handy while reading document position.

TopicDetail
Syntax.offset() / .offset({ top, left }) / .offset(fn)
SinceGetter 1.2; setter & callback 1.4
Getter returns{ top: Number, left: Number }
Relative toDocument (not the offset parent)
BoxBorder box (margins excluded)
vs .position()Document vs offset parent
Static elementsSetter promotes them to relative

Live Preview

Pick an action and see what .offset() returns or sets on the sample box.

Browser demo of .offset() — same idea as the examples below.

Result
$("#box").offset()
→ { top: …, left: … }

jQuery Examples

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

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.
  4. Use. Ideal for tooltips, overlays, and page-level placement.

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.
  4. Verify. Call .offset() again to confirm the move.

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.
  4. Rule. Pick the system that matches where you will place the next element.

Example 4 — Callback Setter (Since 1.4)

Nudge each box from its current document coordinates.

jQuery
$(".box").offset(function (index, coords) {
  return {
    top: coords.top + 10,
    left: coords.left + index * 20
  };
});

$(".box").each(function (i) {
  var o = $(this).offset();
  console.log("box " + i + ": left " + o.left + ", top " + o.top);
});
Try It Yourself

How It Works

  1. Callback. jQuery calls your function once per matched element.
  2. Args. First is the index; second is the current { top, left }.
  3. Return. Return a new coords object to reposition that element.
  4. Pattern. Handy for staggered moves without a manual loop.

Edge Cases & Pitfalls

Check these before trusting .offset() in layout code.

hidden

display:none is unreliable

Hidden elements are not laid out. Show them (or clone off-screen) before measuring.

coords

Do not confuse with .position()

Document coords vs offset-parent coords. Mixing them breaks overlays and drag logic.

static

Setter may change position

Static elements become relative when you set .offset(). Set position yourself if that matters.

fraction

Values may be fractional

Sub-pixel layouts can return numbers like 72.5. Round only when your UI needs integers.

Browser Support

.offset() getter has shipped since jQuery 1.2; the setter and callback forms arrived in jQuery 1.4. Works in all modern browsers with jQuery loaded.

✓ jQuery 1.2+

.offset()

Document-relative coordinates API — getter returns { top, left }; setter accepts an object or function (1.4+).

100% Browser support
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
.offset() Excellent

Bottom line: Use .offset() for document coordinates. Switch to .position() when you need the offset parent instead.

Performance Notes

ApproachCostBest for
.offset()Layout readDocument coordinates
.position()Layout readOffset-parent coordinates
Repeated reads in a loopCan thrash layoutCache { top, left } first

Coordinate getters force layout. Read once, reuse the object, and avoid calling .offset() inside tight animation frames unless needed.

Key Takeaways

  • Role: get/set document-relative { top, left }.
  • vs position: document vs offset parent.
  • Setter: since 1.4; may promote static to relative.
  • Safety: measure visible elements; expect fractional pixels.

One line: .offset() gives document { top, left } — use it for page-level placement, and switch to .position() inside a positioned parent.

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

Learn coordinates relative to the offset parent.

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

9 people found this page helpful