It's .css('width')
that rounds. There's a lot to .css
in jQuery beyond getComputedStyle
. Comparison.
For .css('width')
, jQuery uses offsetWidth
[source]:
val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
In CSS, pixels are just another unit, defined to be 1/96 of an inch. There's nothing inherently integral about them. In high-DPI displays, such as mobile screens, newer laptops, and printed media, it makes sense to have fractions of "pixels." But offsetWidth
is defined to be an integer value [CSSOM]:
readonly attribute long offsetWidth;
In this case, where you explicitly set a width
in the inline style, and you want to get it, one thing you could do is just use plain DOM to get it:
$("#the_box")[0].style.width.replace('px', '')
example
You can also try using getBoundingClientRect
[CSSOM] and working backwards through the changes introduced by borders and padding. This method returns a DOMRect
object with information represented as floating-point numbers [geometry].
$("#the_box")[0].getBoundingClientRect().width - ...