I have a collection of large images in various sizes that I need to display without any stretching inside a container with set height. The challenge is to make the image fit perfectly within the container without distorting its proportions. I must achieve this using HTML elements rather than background images. Below is my code:
.image-container {
height: 420px;
width: 100%;
}
@media (max-width: 480px) {
height: 310px;
}
@media (max-width: 375px) {
height: 275px;
}
@media (max-width: 320px) {
height: 240px;
}
.image-container img {
max-height: 100%;
object-fit: contain;
max-width: 100%;
display: block;
margin: 0 auto;
}
<div class="image-container">
<img src="http://media.gettyimages.com/photos/under-the-tree-picture-id480585809" />
</div>
The current implementation is causing the image to stretch. Does anyone have a solution to rectify this issue? Your suggestions are greatly appreciated.
Thank you in advance.