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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



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

    offset:


    position

    position:

Difference between offset() and postion() 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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.