Open In App

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

Last Updated : 20 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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>
<html>
 
<head>
    <title>The difference between
      offset and position Method</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the method -->
    <script>
        /* code for position */
        $(document).ready(function() {
            $("#position").click(function() {
                var Geek = $("#testDiv").position();
                alert("Top : " + Geek.top +
                    " Left : " + Geek.left);
            });
 
            /* code for offset */
            $("#offset").click(function() {
                var Geek = $("#testDiv").offset();
                alert("Top : " + Geek.top +
                    " Left : " + Geek.left);
            });
        });
    </script>
 
    <style>
        #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;
        }
    </style>
</head>
 
<body>
    <center>
        <h1>GeeksforGeeks</h1>
        <b>offset() and position()</b>
        <div>
            <div id="container">
                <div id="testDiv">
                  Welcome to GeeksforGeeks
                </div>
            </div>
 
            <div style="height:10px"></div>
            <button id="offset">click for offset</button>
            <button id ="position">
                  click for position
            </button>
        </div>
    </center>
</body>
 
</html>


Output:

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

    offset:


    position

    position:

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