views:

411

answers:

4
+1  Q: 

HTML img scaling

I'm trying to display some large images with HTML img tags. At the moment they go off the edge of the screen; how can I scale them to stay within the browser window?

Or in the likely event that this is not possible, is it possible to at least say "display this image at 50% of its normal width and height"?

The width and height attributes distort the image -- as far as I can tell, this is because they refer to whatever attributes the container may end up with, which will be unrelated to the image. I can't specify pixels because I have to deal with a large collection of images each with a different pixel size. Max-width doesn't work.

+6  A: 

Only set the width or height, and it will scale the other automatically. And yes you can use a percentage.

The first part can be done, but requires javascript, so might now work for all users.

IPX Ares
Please, please, please note that doing this on massive images will result in long download times for pages that shouldn't have long download times. It's always better to actually resize the image if possible.
ceejayoz
Thanks! It turns out your solution of only setting the width not only works for correct scaling, but actually also does the first part just by setting the width to 100%.
rwallace
@ceejayoz I agree, but that is not what he is asking.
IPX Ares
A: 

Glancing at the W3 Spec for the img tag, you should be able to do something like:

<img src="img.jpg" width="50%" height="50%"/>

For automatic scaling via Javascript, check this tip

Jweede
A: 

I think the best solution is resize the images via script or locally and upload them again. Remember, you're forcing your viewers to download larger files then they need

RioTera
A: 

The best way I know how to do this, is:

1) set overflow to scroll and that way the image would stay in but you can scroll to see it instead

2) upload a smaller image. Now there are plenty of programs out there when uploading (you'll need something like PHP or .net to do this btw) you can have it auto scale.

3) Living with it and setting the width and height, this although will make it look distorted but the right size will still result in the user having to download the full-sized image.

Good luck!

Dorjan