Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.
To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.
How to fit image without stretching and maintain aspect ratio? Answer: If you want to use the image as a CSS background, there is an elegant solution. Simply use cover or contain in the background-size CSS3 property. contain will give you a scaled-down image.
The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.
If I correctly understand what you want, you may leave the width and height attributes off the image to maintain aspect ratio and use flexbox to do the centering for you.
.fill {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden
}
.fill img {
flex-shrink: 0;
min-width: 100%;
min-height: 100%
}
<div class="fill">
<img src="https://picsum.photos/id/237/320/240" alt="" />
</div>
JSFiddle here
I tested this successfully in IE9, Chrome 31, and Opera 18. But no other browsers were tested. As always you must consider your particular support requirements.
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.
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;
}
This should do it:
img {
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
}
An old question but deserves an update as now there is a way.
The correct CSS based answer is to use object-fit: cover
, which works like background-size: cover
. Positioning would be taken care of by object-position
attribute, which defaults to centering.
But there is no support for it in any IE / Edge browsers, or Android < 4.4.4. Also, object-position
is not supported by Safari, iOS or OSX. Polyfills do exist, object-fit-images seems to give best support.
For more details on how the property works, see CSS Tricks article on object-fit
for explanation and demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With