Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 396
Qcom Avatar asked Aug 03 '10 12:08

Qcom


People also ask

How do you scale an image proportionally in HTML?

Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the parent container width. We can further use CSS to fix the aspect ratio of the image, learn more about presenting images in a specific aspect ratio.

How do I change the width of an image in percentage?

Try It: Set the width of an image using a percentage and resize your browser window. For the width attribute, you can also use percentages, where 100% is the total space available. So, if you wanted an image to be one quarter the width of the window, you could set width to 25% (one quarter of 100% is 25%).

How do you maintain aspect ratio in HTML?

In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.

How do I set the same width and height of an image in HTML?

container img { width: 100%; height: 400px; //this should be the same as the width value.. }


1 Answers

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 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.

like image 105
Pat Avatar answered Oct 08 '22 01:10

Pat