2

When I set the height of an image 100%, the width is calculated automatically in order to keep the image aspect ratio.
Most of the times the width of such image is not an integer but double (like 645.34).
$("#myImg").width() returns me always an integer.
How can I get the exact width of such an image using jquery?

Naor
  • 23,465
  • 48
  • 152
  • 268
  • 1
    The width() returned value is in "pixel". I don't see how your image could have a non-integer width value. – koopajah Dec 02 '12 at 09:33
  • @koopajah: float value is non-integer value. When you put am image 100% height for example, the img width will not be integer. Maybe this will interest you: http://stackoverflow.com/questions/13655762/i-need-to-add-one-pixel-to-parent-div – Naor Dec 02 '12 at 09:53
  • I know a float is not an integer. What I'm saying is that you image will never have a float width or a float height. They are based on pixels, and you can not have an image with a width of 100 and a half pixels. It will always be an integer – koopajah Dec 02 '12 at 09:54

1 Answers1

1

The actual applied width will always be equated to an integer value of pixels by the browser. There is no renderable half-pixel. We can specify 22.5px etc, but it will be rounded to the nearest integer by the browser. Some browsers may however anti-alias the edge pixels to simulate half-pixels, still the applied width/height value will always be an integer.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • So if I have 10 images one after another and I want them to be displayed one by one in one line with no spaces between, how can I calculate the width of all the images? Isn't it possible? – Naor Dec 02 '12 at 09:52
  • You can use `.width()` on each image and sum them. Also note that you can use `.outerWidth()` to control whether you want the calculated width to include margins (if any) as well. – techfoobar Dec 02 '12 at 09:53
  • as yo can see here: http://stackoverflow.com/questions/13655762/i-need-to-add-one-pixel-to-parent-div summing the widths is not working and is not a solution.. – Naor Dec 02 '12 at 13:26