HTML img scaling
CSS is enough:
img {
width : desired_width;
height: auto; /*to preserve the aspect ratio of the image*/
}
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.
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
I know that this question has been asked for a long time but as of today one simple answer is:
<img src="image.png" style="width: 55vw; min-width: 330px;" />
The use of vw in here tells that the width is relative to 55% of the width of the viewport.
All the major browsers nowadays support this.
Check this link.