1

When I set a CSS property to a float value, like so:

​$('#div').css('left', 123.1234567 );

Inspecting #div in the DOM shows that left was set to 123.1234567px, however all three of these:

console.log($('#div').css('left'), $('#div').position(), $('#div').offset() );

return the integer value of 123px.

Is jQuery trying to be too smart, or is it masking a frailty in the browsers? Do browsers support sub-pixel positioning of divs? Would they if I were using a canvas tag? ​

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
BishopZ
  • 6,269
  • 8
  • 45
  • 58
  • jQuery defaults to px when there is no unit. AFAIK, there is also no fractional pixels. – Joseph Apr 27 '12 at 05:42
  • This might help in regards to fractional positioning: http://stackoverflow.com/questions/4308989/are-the-decimal-places-in-a-css-width-respected – Christian Apr 27 '12 at 05:43
  • @Christian Varga: That question has nothing to do with jQuery's implementation of DOM styling whatsoever. Not a duplicate. – BoltClock Apr 27 '12 at 05:43
  • Sure, but it does refer to sub-pixel positioning, which is the *real* question being asked. Edited for the op. – Christian Apr 27 '12 at 05:45

2 Answers2

1

I believe this is related to the known bug in jQuery. See http://bugs.jquery.com/ticket/9628.

I had the same issue with jQuery's height().

Why does jQuery's height() automatically rounds up the value?

Community
  • 1
  • 1
CookieEater
  • 2,237
  • 3
  • 29
  • 54
-1

I'm not sure why your code is not working, the one I tried on JSFiffle seems to be working fine.

http://jsfiddle.net/JBDkr/2/

KBN
  • 2,915
  • 16
  • 27
  • I just did, and it alerts "1235.45px" . – KBN Apr 27 '12 at 06:32
  • Hmm, I'm not sure then :). Though I'd like to know why would you specify pixels in floating points, how is a 0.5px rendered ? 1px being the smallest unit. You can specify floating points if you give the width in percentage or "em" units. – KBN Apr 27 '12 at 06:36