Specifying width and height as percentages without skewing photo proportions in HTML

HtmlImageImage Size

Html Problem Overview


I was wondering if in the width and height <img> attributes, I could specify width and height as percentages?

Well, I guess that is obvious, because when I try so, it resizes, but it appears to skew the quality of my image.

Here is an example of my markup with fixed attributes:

<img src="#" width="346" height="413">

Now, while trying to scale this down, say by half, via percentages:

<img src="#" width="50%" height="50%">

I get something completely different than:

<img src="#" width="173" height="206.5">

I think I'm just fundamentally mistaking my percentage markup or something because there is a noticeable difference between my second and third example visually.

Html Solutions


Solution 1 - Html

Note: it is invalid to provide percentages directly as <img> width or height attribute unless you're using HTML 4.01 (see current spec, obsolete spec and [this answer][3] for more details). That being said, browsers will often tolerate such behaviour to support backwards-compatibility.

Those percentage widths in your 2nd example are actually applying to the container your <img> is in, and not the image's actual size. Say you have the following markup:

<div style="width: 1000px; height: 600px;">
    <img src="#" width="50%" height="50%">
</div>

Your resulting image will be 500px wide and 300px tall.

jQuery Resize

If you're trying to reduce an image to 50% of its width, you can do it with a snippet of jQuery:

$( "img" ).each( function() {
    var $img = $( this );
    $img.width( $img.width() * .5 );
});

Just make sure you take off any height/width = 50% attributes first.

[3]: https://stackoverflow.com/questions/26100484/what-are-the-consequences-of-using-percentage-for-width-height-attributes-of-an%20for%20more%20details)

Solution 2 - Html

You can set one or the other (just not both) and that should get the result you want.

<img src="#" height="50%">

Solution 3 - Html

Here is the difference:

This sets the image to half of its original size.

<img src="#" width="173" height="206.5">

This sets the image to half of its available presentation area.

<img src="#" width="50%" height="50%">

For example, if you put this as the only element on the page, it would attempt to take up 50% of the width of the page, thus making it potentially larger than its original size - not half of its original size as you are expecting.

If it is being presented at larger than original size, the image will appear greatly pixelated.

Solution 4 - Html

Try use scale property in css3:

75% of original:

-moz-transform:scale(0.75);
-webkit-transform:scale(0.75);
transform:scale(0.75);

50% of original:

-moz-transform:scale(0.5);
-webkit-transform:scale(0.5);
transform:scale(0.5);

Solution 5 - Html

width="50%" and height="50%" sets the width and height attributes to half of the parent element's width and height if I'm not mistaken. Also setting just width or height should set the width or height to the percentage of the parent element, if you're using percents.

Solution 6 - Html

Given the lack of information regarding the original image size, specifying percentages for the width and height would result in highly erratic results. If you are trying to ensure that an image will fit within a specific location on your page then you'll need to use some server side code to manage that rescaling.

Solution 7 - Html

From W3Schools

The height in percent of the containing element (like "20%").

So I think they mean the element where the div is in?

Solution 8 - Html

There is actually a way to do this with html only. Just sets:

<img src="#" width height="50%">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQcomView Question on Stackoverflow
Solution 1 - HtmlPatView Answer on Stackoverflow
Solution 2 - HtmlFoscoView Answer on Stackoverflow
Solution 3 - HtmlFentonView Answer on Stackoverflow
Solution 4 - HtmlNalan MadheswaranView Answer on Stackoverflow
Solution 5 - HtmlpkaukoView Answer on Stackoverflow
Solution 6 - HtmlLazarusView Answer on Stackoverflow
Solution 7 - HtmlJavaPeteView Answer on Stackoverflow
Solution 8 - HtmlGilgameshView Answer on Stackoverflow