Search code examples
jqueryposition

jQuery: Obtain the exact position of a DIV in pixels instead of getting back "auto" as the result


Let's say I have 5 divvary in height and width with float:left;:

When I use .css(""); to get their positions (left & right),

"auto" would always end up be the result.

Any method to get the exact number (left & right) in pixels relative to the container ( position:absolute;)?

(there are possibilities that the div would start a new row as I use float:left; and their widths and heights are undeterminable.)


Solution

  • jQuery has two methods for positions, one relative to document, and one relative to container.

    Relative to container (position).

    Relative to document (offset).

    Note that position says relative to offset parent, so you might have to apply position: relative to the container element.