HTML img scaling

南笙酒味 提交于 2019-11-29 21:59:12
RiddlerDev

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 not work for all users.

Jon W

Glancing at the W3 Schools reference 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

css is enough :

width : desired_width;
height: auto;/*to preserve the aspect ratio of the image*/

No Javascript required.

IE6 Internet Explorer 6

Percent only works for the width of an element, but height:100%; does not work without the correct code.

CSS

html, body { height:100%; } 

Then using a percentage works properly, and dynamically updates on window resize.

<img src="image.jpg" style="height:80%;">

You do not need a width attribute, the width scales proportionately as the browser window size is changed.

And this little gem, is in case the image is scaled up, it will not look (overly) blocky (it interpolates).

img { -ms-interpolation-mode: bicubic; }

Props go to this source: Ultimate IE6 Cheatsheet: How To Fix 25+ Internet Explorer 6 Bugs

Adding max-width: 100%; to the img tag works for me.

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 than they need

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!