I have a link to jsFiddle that will present the problem. Basically, the problem is in the middle container (sector2
the blue one) which flexes in column orientation and has 5 flex child elements. Inside four of these elements are images with the size of 150x150px. I have given the image-wrapper
(class of every child element) to flex: 1
, which means that the blue container should be divided into 5 equal height parts. Now I set the min-height: 100%
of every image, which makes the height of the image to be as much as one part (child element) height is.
The problem arises when I want to maintain the aspect ratio of the image and set the object-fit: scale-down
. This property makes the image preserve the aspect ratio, but the containers (image-wrapper
) and the blue container (sector2
) still hold the original image width of 150px.
My question is: how can I preserve the image aspect ratio and in the same time containers should auto resize based on the content?
I will additionally add the HTML and CSS code here:
.container {
display: flex;
flex-direction: row;
width: 600px;
height: 500px;
background-color: red;
}
.sector1 {
display: flex;
flex-direction: row;
flex: 1 0 0%;
background-color: green;
}
.sector3 {
display: flex;
flex-direction: row;
flex: 1 0 0%;
background-color: green;
}
.sector2 {
display: flex;
flex-direction: column;
flex: 0 0 auto;
background-color: blue;
margin: 0 6px;
}
.image-wrapper {
display: flex;
flex-direction: column;
flex: 1 0 0%;
min-height: 0;
min-width: 0;
}
.image {
object-fit: scale-down;
min-height: 100%;
width: auto;
}
<div class="container">
<div class="sector1"></div>
<div class="sector2">
<div class="image-wrapper">
<img class="image" src="https://mk0powerpackele6qayu.kinstacdn.com/wp-content/uploads/2018/07/image-75-150x150.jpg" />
</div>
<div class="image-wrapper">
<img class="image" src="https://mk0powerpackele6qayu.kinstacdn.com/wp-content/uploads/2018/07/image-75-150x150.jpg" />
</div>
<div class="image-wrapper"></div>
<div class="image-wrapper">
<img class="image" src="https://mk0powerpackele6qayu.kinstacdn.com/wp-content/uploads/2018/07/image-75-150x150.jpg" />
</div>
<div class="image-wrapper">
<img class="image" src="https://mk0powerpackele6qayu.kinstacdn.com/wp-content/uploads/2018/07/image-75-150x150.jpg" />
</div>
</div>
<div class="sector3"></div>
</div>