I have an img inside a div tag, and currently I am using the CSS
img {
max-height: 100%;
max-width: 100%;
}
This currently keeps the images fitting inside the div, which is what I wanted. However, if the image file is smaller than the div, the image will not be the maximum size it can be. Is there an easy way to maximise the image, while keeping the image inside the div, and keeping the original aspect ratio?
I've tried setting the height to 100%, with a max-width of 100%, but this distorts the image, which is not what I'm looking for.
I also tried object-fit: contain;
, but this doesn't seem to do anything.
Thanks :)
max-width
will limit the image to 100% of the image's native width. What that means is, if your image has a width of 400px and the div has a width of 600px, then the image will not fill the full width of the div as it can only stretch to 400px because ofmax-width
. Are any of you're DIVs fixed width/height? – hungerstar