Position Methods

The following methods are helpful in determining the exact positioning of elements—in relation to a positioned ancestor, the document body, or the viewable area of the document.

As in the Size Methods section, we'll be using the same basic HTML for each of the following examples:

<body>
  <div id="container">
<!-- CODE CONTINUES -->    
    <div id="content">
      <div class="dim-outer">
        <p>This is the outer dimensions box. It has the following CSS rule:</p>
<pre><code>.dim-outer {
  height: 200px;
  width: 200px;
  margin: 10px;
  padding: 1em;
  border: 5px solid #e3e3e3;
  overflow: auto;
  font-size: 12px;
}</code></pre>
        <p>Scroll down for the inner dimensions box.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><div class="dim-inner"> This is the inner dimensions box.
        </div>
      </div> 

<!-- CODE CONTINUES -->

    </div>
  </div> 
</body>

Gets the number of pixels that the window or a scrollable element within the document has been scrolled down.

.scrollTop()

Sets the number of pixels to be scrolled down in the window or or the matched set of scrollable element within a document.

.scrollTop(value)

Gets the number of pixels that the window or a scrollable element within the document has been scrolled from left to right.

.scrollLeft()

Sets the number of pixels to be scrolled from left to right in the window or the matched set of scrollable elements within a document.

.scrollLeft(value)

Gets the top and left coordinates of the first element in the set of matched elements. Also gets the matched element's scrollTop and scrollLeft offsets.

.offset([options])
.offset(options, returnObject)

The .offset method allows us to locate the top and left positions of any element anywhere on the page, whether its position is static or relative, absolute or fixed, and regardless of the position of scrollbars. With options for factoring margin, border, padding, and scroll into the calculation, .offset() provides great flexibility as well as accuracy.

The following series of images demonstrates the different values returned by .offset() depending on how the options are set.

Defaults

In the first example, the default settings for padding (false), border (false), and margin (true) are used. The result:

Description.offset()about

Note here that since the default for margin is true, the distance from the left edge of the window to the matched element extends all the way to (but not including) the element's border.

Including Border

In the second example, the border option is set to true. Since <div class="dim-outer"> has a 5-pixel border around it, the top and left values increase by 5 pixels each:

Description.offset()about

Including Border and Padding

The next example sets both the border and padding options to true (remember that the margin option's value is true by default). The result is an increase, again, of 5 pixels for the borders and another 12 pixels (1em) for the padding:

Description.offset()about

Finding the Position Relative to an Ancestor

With the relativeTo option, we can find the offset distance between an element and any one of its positioned ancestors. In the next example, we're getting the offset between <div class="dim-outer"> and <div id="content">. Since this content <div> is itself offset from the left side of the window due to a container's 24-pixel left margin, the value of left is now 24 pixels less than that of the previous example:

Description.offset()about

It's worth noting here that, since the relativeTo setting takes a DOM element, we used the shorthand [0] notation to convert a jQuery object to a DOM element before using it for the relativeTo argument.

The top value of 27 is derived from the sum of the floated <div class="dim-outer"> element's margin (12), border (5), and padding (10). If <div id="content"> had any top padding applied to it, that would be added to the total top offset as well.

Returning Scroll Offsets

The scroll option, which has a default value of true, is particularly useful when the matched element is inside one or more elements that have the overflow property set to auto or scroll. It adds the total scroll offsets of all ancestor elements to the total offset and adds two properties to the returned object, scrollTop and scrollLeft. Its usefulness can be observed in the following example showing the offset of <div class="dim-inner"> when <div class="dim-outer"> has been scrolled down 79 pixels:

Description.offset()about

Maintaining Chainability

If we wish to pass in a return object in order to continue chaining methods, we must still include the options map. To keep the default values intact for those options while passing in a return object, we can simply use an empty map. For example, $('div.dim-outer').offset({}, returnObject) obtains the same values as $('div.dim-outer').offset(), but stores them in returnObject for later use.

Suppose we want to get the offset and scroll values of <div class="dim-outer"> while changing its background color to gray (#cccccc) at the same time. The code would look like this:

We start by declaring a variable for the return object (retObj). Then we chain the .offset and .css methods to the selector. Finally, we do something with the object returned by .offset()—in this case, log the results with our Log plug-in. The <div>'s background color is changed and the .offset() values are logged as follows:

Description.offset()about

Gets the position of the first element in the matched set of elements, relative to its nearest relative-, absolute- or fixed-positioned ancestor.

.position()
.position(returnObject)