24

I am rendering an image into a div. I want to avoid stretching of my image.

div {
  height: 300px;
  width: 300px; 
}
img {
  min-width: 300px;
  min-height: 300px;
  max-height: 300px;   
}

My problem is that my image's width stretches. I want it to have the regular width even though parts of the image will be missing.

div {
  height: 300px;
  width: 300px; 
  overflow: hidden;
}
img {
  height: 300px
  max-width: none;
  min-width: 300px;
}
BarryCap
  • 168
  • 1
  • 11
Zincktest
  • 649
  • 2
  • 6
  • 9

10 Answers10

46

You can achieve this with simply adding object-fit: cover;. An example might be like -

img {
    height: 300px
    width: 100%;
    object-fit: cover;
}
Al Amin
  • 739
  • 6
  • 12
  • This is a good solution if you want to keep your min/max width and height, which can be useful for things like setting a hero image to 100vh. I've also found this helpful for situations where Safari would squish my images when no other browser did. – Pwpwpw Dec 15 '19 at 18:39
  • 1
    Note: IE11 doesn't support `object-fit`. – Neurotransmitter Jun 11 '20 at 16:16
  • IE is dead .... After many years as the gold standard of internet browsers, Microsoft is phasing out Internet Explorer, officially ending support on August 17, 2021. – Gass Mar 28 '22 at 06:52
13

I would forget setting the min-height and the max-height. Just set the height to be 300 pixels and put an overflow hidden on the div tag. That way no matter what the image size it will always stay in proportion and never go outside the boundaries of your div tag.

div { height: 300px; width: 300px; overflow: hidden; }
img { height: 300px; }
ZombieCode
  • 1,597
  • 2
  • 22
  • 43
4

Put the image as the div background if you want to avoid stretching the easiest way (yet maintain the original width).

Shomz
  • 36,910
  • 3
  • 55
  • 83
3

To make it more flexible as just using 300px use:

img { width: 100%; object-fit: cover; }

Height is automatically adjusted

Vincent
  • 4,248
  • 1
  • 37
  • 37
2

just specify max-width 100% and height :auto

1

Use max-width instead of min-width, and just set height to 300px (or only use max-height).

justisb
  • 6,652
  • 2
  • 26
  • 43
1

You can use overflow:hidden to hide any portion of the image outside of the width of the div.

div {
       height: 300px;
       width: 300px; 
       overflow: hidden;
    }
img {
       /*min-width: 300px;*/
       height: 300px;   
    }
snumpy
  • 2,748
  • 5
  • 23
  • 38
0

==>If you are gonna have fixed height and don't want width stretched

div {
  height: 300px;
  width: 300px; 
  overflow: hidden;
}
img {
  height: 300px
}

==>If you are gonna have fixed width and don't want height stretched

div {
   height: 300px;
   width: 300px; 
   overflow: hidden;
}
img {
 width: 300px
}
Melkong
  • 1
  • 1
0

After giving the image a fixed height and width, I added object-fit as below:

img {
  width: 300px;
  height: 300px;
  object-fit: contain;
}
Mundruku
  • 89
  • 1
  • 5
0

To avoid the image from resizing use:

object-fit: none;

More about object-fit

The CSS object-fit property is used to specify how an or should be resized to fit its container.

This property tells the content to fill the container in a variety of ways; such as "preserve that aspect ratio" or "stretch up and take up as much space as possible".

Object-fit Values

  • fill: this is default. The image is resized to fill the given dimension. If necessary, the image will be stretched or squished to fit.
  • contain: the image keeps its aspect ratio, but is resized to fit within the given dimension
  • cover: the image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit
  • none: the image is not resized scale-down - the image is scaled down to the smallest version of none or contain.

More info and examples

Gass
  • 4,098
  • 2
  • 13
  • 27