Gaurav Dadhania
Gaurav Dadhania

Reputation: 5327

Why does jQuery .css('left') and .position().left return different values?

The values I'm getting for $(el).css('left') and $(el).position().left are different?

If I go $(el).css('left', '100px'), then $(el).css('left') it returns 110px instead of 100px (yes, it is always 10% more) and if I evaluate $(el).position().left, it gives me 100.

Why does Chrome behave this way? You can see how this would affect jQuery animations using the left property.

I'm using Chrome 21.0.1180.57 on Ubuntu.

EDIT 1: Seems to be only affecting Chrome, FF 14.0.1 is giving me the same values.

Upvotes: 15

Views: 58126

Answers (2)

Gabriel Silveira
Gabriel Silveira

Reputation: 372

Try this code :

$("#div")[0].style.left

Upvotes: 3

Esailija
Esailija

Reputation: 140210

The left returns calculated value of the CSS left property.

position().left returns the x-coordinate relative to the element's first offset parent.

These values can be equal and they can be not equal .

position().left can also easily be different between browsers because of different rendering whereas .css("left") can only differ in the units given, if that.

Upvotes: 21

Related Questions