I found this thread — How do you stretch an image to fill a
I h
The only way I achieved the "best case" scenario described, was putting the image as a background:
<div class="container"></div>
.container {
width: 150px;
height: 100px;
background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: 50% 50%;
}
It's a bit late but I just had the same problem and finally solved it with the help of another stackoverflow post (https://stackoverflow.com/a/29103071).
.img {
object-fit: cover;
width: 50px;
height: 100px;
}
Hope this still helps somebody.
Ps: Also works together with max-height, max-width, min-width and min-height css properties. It's espacially handy with using lenght units like 100% or 100vh/100vw to fill the container or the whole browser window.
Just fix the height of the image & provide width = auto
img{
height: 95vh;
width: auto;
}
You can use div to achieve this. without img tag :) hope this helps.
.img{
width:100px;
height:100px;
background-image:url('http://www.mandalas.com/mandala/htdocs/images/Lrg_image_Pages/Flowers/Large_Orange_Lotus_8.jpg');
background-repeat:no-repeat;
background-position:center center;
border:1px solid red;
background-size:cover;
}
.img1{
width:100px;
height:100px;
background-image:url('https://images.freeimages.com/images/large-previews/9a4/large-pumpkin-1387927.jpg');
background-repeat:no-repeat;
background-position:center center;
border:1px solid red;
background-size:cover;
}
<div class="img">
</div>
<div class="img1">
</div>
.image-wrapper{
width: 100px;
height: 100px;
border: 1px solid #ffffd;
}
.image-wrapper img {
object-fit: contain;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
}
<div class="image-wrapper">
<img src="">
</div>
All answers below have fixed width and height, which makes solution "not responsive".
To achieve the result but keep image responsive I used following:
HTML:
<div class="container">
<img style="background-image: url("https://i.imgur.com/XOmNCwY.jpg");" src="img/blank.gif">
</div>
.container img{
width: 100%;
height: auto;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}