How do I auto-resize an image to fit a 'div' container?


How do you auto-resize a large image so that it will fit into a smaller width div container whilst maintaining its width:height ratio?

Example: - when an image is inserted onto the editor panel and the image is too large to fit onto the page, the image is automatically resized.

3/27/2019 10:34:18 AM

Accepted Answer

Do not apply an explicit width or height to the image tag. Instead, give it:


Also, height: auto; if you want to specify a width only.


img {
    max-width: 100%;
    max-height: 100%;

.portrait {
    height: 80px;
    width: 30px;

.landscape {
    height: 30px;
    width: 80px;

.square {
    height: 75px;
    width: 75px;
Portrait Div
<div class="portrait">
    <img src="">

Landscape Div
<div class="landscape">
    <img src="">

Square Div
<div class="square">
    <img src="">

5/14/2018 10:18:33 PM


It turns out there's another way to do this.

<img style='height: 100%; width: 100%; object-fit: contain'/>

will do the work. It's CSS 3 stuff.



Currently there is no way to do this correctly in a deterministic way, with fixed-size images such as JPEGs or PNG files.

To resize an image proportionally, you have to set either the height or width to "100%", but not both. If you set both to "100%", your image will be stretched.

Choosing whether to do height or width depends on your image and container dimensions:

  1. If your image and container are both "portrait shaped" or both "landscape shaped" (taller than they are wide, or wider than they are tall, respectively), then it doesn't matter which of height or width are "%100".
  2. If your image is portrait, and your container is landscape, you must set height="100%" on the image.
  3. If your image is landscape, and your container is portrait, you must set width="100%" on the image.

If your image is an SVG, which is a variable-sized vector image format, you can have the expansion to fit the container happen automatically.

You just have to ensure that the SVG file has none of these properties set in the <svg> tag:


Most vector drawing programs out there will set these properties when exporting an SVG file, so you will have to manually edit your file every time you export, or write a script to do it.


Here is a solution that will both vertically and horizontally align your img within a div without any stretching even if the image supplied is too small or too big to fit in the div.

The HTML content:

<div id="myDiv">
  <img alt="Client Logo" title="Client Logo" src="Imagelocation" />

The CSS content:

  height: 104px;
  width: 140px;
#myDiv img
  max-width: 100%;
  max-height: 100%;
  margin: auto;
  display: block;

The jQuery part:

var logoHeight = $('#myDiv img').height();
    if (logoHeight < 104) {
        var margintop = (104 - logoHeight) / 2;
        $('#myDiv img').css('margin-top', margintop);

Make it simple!

Give the container a fixed height and then for the img tag inside it, set width and max-height.

<div style="height: 250px">
     <img src="..." alt=" " style="width: 100%;max-height: 100%" />

The difference is that you set the width to be 100%, not the max-width.


A beautiful hack.

You have two ways of making the image responsive.

  1. When an image is a background image.
    width: 300px;
    height: 300px;
    background-image: url(;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;

<div id="container"><div>

Run it here

But one should use img tag to put images as it is better than background-image in terms of SEO as you can write keyword in the alt of the img tag. So here is you can make the image responsive.

  1. When image is in img tag.
    max-width: 400px;
    overflow: hidden;
    width: 100%;
    object-fit: contain;

<div id="container">
   <img src="" alt="your_keyword"/>

Run it here


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]