I have a div with a max-height. It contains an image with a portrait aspect ratio. My goal is, that the image should adapt to the size of the container (getting max its height and max. it’s width), without losing it’s aspect ratio.
When I use height: auto; width: 100%
on the image, the image gets larger than its parents max-height
.
On the other hand, if I set width: auto; height: 100%
on the image, it can get larger than its parents width.
And if I set max-height: 50px; width: 100%;
it can get distorted, depending on its parents width.
.container {
max-height: 50px;
border: 2px solid red;
}
img {
width: 100%;
height: auto
}
<div class="container">
<img src="https://placehold.co/40x60.jpg" width="40" height="60"/>
</div>
2
Answers
Maybe with CSS grid