I am trying to keep image aspect ration correct when either width > height OR height > width. I am having issue doing this when the width exceeds height, the image will overflow into the parent container. I dont want this to happen, instead readjust the image size to still keep the correct aspect ratio.
This example has an image of 640 x 480, with the ratio of 4:3
If i drag the container width smaller than the height i need even padding top/bottom without image overflow. If i drag the container wider than the height then i need even padding left/right without image overflow.
.main {
height: 100%;
width: 100%;
}
.wrapper {
position: relative;
padding-bottom: 75%;
}
.wrapper img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="main">
<div class="wrapper">
<img src="https://placeimg.com/640/480/animals" />
</div>
</div>
