Example

Get the current offset position of a p element:

$("button").click(function(){
  x=$("p").offset();
  alert("Left offset: " + x.left + " Top offset: " + x.top);
});

Try it yourself »

Definition and Usage

The offset() method set or returns the offset (position) for the selected elements, relative to the document.


Return Offset Coordinates

Returns the offset coordinates of the FIRST matched element.

This method returns an object with 2 properties, top and left, which represent the top and left positions in pixels.

Syntax

$(selector).offset()
Try it yourself »


Set Offset Coordinates

Sets the offset coordinates of ALL matched elements.

Syntax

$(selector).offset(value)
Try it yourself »

Parameter Description
value Required. Specifies the top and left coordinates in pixels.

Possible values:

  • Value pair, like {top:100,left:0}
  • An object with top and left properties


Set Offset Coordinates Using a Function

Using a function to set the offset coordinates of ALL matched elements.

Syntax

$(selector).offset(function(index,oldoffset))
Try it yourself »

Parameter Description
function(index,oldoffset) Specifies a function that returns an object containing the top and left coordinates.
  • index - Optional. Receives the index position of the selector
  • oldoffset - Optional. Receives the current coordinates of the selector


Examples

Try it Yourself - Examples

Set an offset position for an element using an object
How to set the offset position for an element using a new object.

Set an offset position for an element using the offset position of another element
How to set the offset position for an element using the offset position of an existing element.