How do you stretch an image to fill a while keeping the image’s aspect-ratio?

I need to make this image stretch to the maximum size possible without overflowing it’s <div> or skewing the image.

I can’t predict the aspect-ratio of the image, so there’s no way to know whether to use:

<img src="https://stackoverflow.com/questions/1891857/url" style="width: 100%;">

or

<img src="https://stackoverflow.com/questions/1891857/url" style="height: 100%;">

I can’t use both (i.e. style=”width: 100%; height: 100%;”) because that will stretch the image to fit the <div>.

The <div> has a size set by percentage of the screen, which is also unpredictable.

19 Answers
19

Leave a Comment