0

Is there a way to prevent the stretching of an image, to crop it somehow but keep the dimensions..?

img {
  width: 90px;
  height: 120px;
}
<img src="https://i.pinimg.com/736x/d4/c3/dc/d4c3dc946ae1e087d24def574215e266--image-search-google-search.jpg" alt="">
Nawaz Ghori
  • 591
  • 6
  • 20
DevJoe
  • 413
  • 1
  • 7
  • 20

3 Answers3

2

If you want to make the image fit a defined container with fixed dimensions, you can use object-fit:cover:

div {
  height: 120px;
  width: 90px;
}

img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<div>
  <img src="https://i.pinimg.com/736x/d4/c3/dc/d4c3dc946ae1e087d24def574215e266--image-search-google-search.jpg" alt="">
</div>
sideroxylon
  • 4,338
  • 1
  • 22
  • 40
  • Thanks, I was thinking about using this property but it's not working in IE, is there something other way that you're aware of? – DevJoe Nov 15 '17 at 11:27
  • https://stackoverflow.com/questions/37792720/ie-and-edge-fix-for-object-fit-cover – Jamie Barker Nov 15 '17 at 11:57
0

To keep the exact same dimensions of this image you should only set one of the two dimensions. The other one will automatically be calculated:

img {
  width: 90px;
  /*height: 120px;*/
}
<img src="https://i.pinimg.com/736x/d4/c3/dc/d4c3dc946ae1e087d24def574215e266--image-search-google-search.jpg" alt="">

Hope I helped out.

Puka
  • 1,485
  • 1
  • 14
  • 33
0

Instead of using width and height, use max-width and max-height.

img {
  max-width:90px;
  max-height:120px;
}

DEMO

Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54