views:

42

answers:

1

Hi everyone, I've searched around and couldn't find this. I'm trying to get the width of a div, but if it has a decimal point it rounds the number.

example:

#container{
background: blue;
width: 543.5px;
height: 20px;

margin: 0;
padding: 0;

}

if I do $('#container').width(); it will return 543 instead of 543.5. How do I get it to not round the number and return the full 543.5 (or whatever number it is).

Thanks.

+2  A: 

I don't think browsers will use fractional dimensions internally. The .width() returns the computed width, which the rendering engine should have already rounded to integer.

You could use .css('width'), which should return "543.5px" on some browsers (e.g. Firefox (Gecko)).

KennyTM
Ah I believe you're right, thank you. Will mark as answered when I can.
MoDFoX
Tested this, and doesn't seem to work. http://jsfiddle.net/KzhxM/ Result is still rounded.
patrick dw
You're right patrick, but as Kenny said, I believe both .width() and .css('width') return the computed style which is rounded by the browser.
MoDFoX
@MoDFoX: it will return the raw value on Firefox (Gecko), but not on Opera (Presto). There's no way to get back the original fractional value with `.width()` or `.css()` reliably.
KennyTM
Thanks for your help.
MoDFoX
@MoDFox - Sorry, I must have missed the point of your question.
patrick dw