Open In App

What is the difference between position() and offset() in jQuery ?

Last Updated : 20 Jun, 2021
Like Article

Both the jQueryUI method the returns the object which contains integer co-ordinate properties of the top and left positions. The positions of top and left coordinates are returned in pixels. Both functions are applied only on visible elements, not on hidden elements.

Example: The example gives top and left co-ordinates of the box which contains the text.

<!DOCTYPE html>
    <title>The difference between
      offset and position Method</title>
    <script src=
    <!-- jQuery code to show the method -->
        /* code for position */
        $(document).ready(function() {
            $("#position").click(function() {
                var Geek = $("#testDiv").position();
                alert("Top : " + +
                    " Left : " + Geek.left);
            /* code for offset */
            $("#offset").click(function() {
                var Geek = $("#testDiv").offset();
                alert("Top : " + +
                    " Left : " + Geek.left);
        #container {
            width: 40%;
            min-height: 100px;
            padding: 20px;
            font-size: 25px;
            border: 2px solid green;
            font-weight: bold;
            color: green;
            background: gray;
            position: relative;
        #testDiv {
            background: silver;
        h1 {
            color: green;
        <b>offset() and position()</b>
            <div id="container">
                <div id="testDiv">
                  Welcome to GeeksforGeeks
            <div style="height:10px"></div>
            <button id="offset">click for offset</button>
            <button id ="position">
                  click for position


  • Before clicking any button:
  • After clicking the button:




Difference between offset() and position() Method:

offset() Method position() Method
The offset() method in jQuery returns the first found position of HTML element with respect to the document. The position() method in jQuery returns the current position of HTML element with respect to its offset parent.
The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element.
When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method. When you want to position a new element near another DOM element within the same container, its better to use jQuery position() method.
The offset() method is mostly used in drag-and-drop functions. The position() method is used to position an element relative to document, window or other elements like mouse or cursor.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads