W3Schools.com

jQuery CSS offset() Method

jQuery CSS Methods jQuery CSS Methods

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.


jQuery CSS Methods jQuery CSS Methods


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE