Problem
Say we have a vertical (i.e. flex-direction: column) flexbox container with a given width and height. The flexbox contains divs and each div contains an image.
- All divs and images are supposed to shrink/grow by the same percentage to fill the height of the flexbox, which is achieved by using flex-shrink and/or flex-grow.
- All images are supposed to keep their aspect ratio (i.e. no stretching), which is achieved by leaving their css "width" attribute unset.
- Each div is supposed to have the same width as the image that's inside of it, which I expected to achieve with "width: min-content" or "width: max-content" or "width: fit-content", but this does not work.
How can the third point be achieved?
Note: It seems to work outside of flexbox.
Example
In the example below the div with id "imagecontainer" does not reduce its width when its height is shrunk below flex-basis, even though its content reduces in width. However, having the same div outside of a flexbox makes it reduce its width correctly when it forces its child image to become smaller through height constraints.
How do I make the "imagecontainer" div in the flexbox scale its width with the image (so that the red div in the first example has the same size as the red div in the third example)?
Image in flex container with limited height
<div style="display: flex; flex-direction: column; border: 1px solid black; height: 110px; width: 200px; align-items: flex-start;">
<div id="imagecontainer" style="position: relative; flex-shrink: 1; flex-basis: 150px; min-height: 0; box-sizing: border-box; border: 1px solid red; width: min-content;">
<img src="https://via.placeholder.com/150.png" style="max-height: 150px; height: 100%;" />
</div>
</div>
Original image<br />
<img src="https://via.placeholder.com/150.png" style="border: 1px solid black;" />
<br />
Image in just a simple div with limited height
<div style="position: relative; height: 110px; border: 1px solid red; width: min-content;">
<img src="https://via.placeholder.com/150.png" style="max-height: 150px; height: 100%;" />
</div>
Edit:
Based on one of the answers below, this is a version that solves the problem on Chrome but doesn't work on Firefox. The only significant change from the example above to make it work was to add "height: 100%" on the "imagecontainer" div.
Image in flex container with limited height
<div style="display: flex; flex-direction: column; border: 1px solid black; height: 110px; width: 200px; align-items: flex-start;">
<div id="imagecontainer" style="position: relative; flex-shrink: 1; flex-basis: 120px; min-height: 0; box-sizing: border-box; border: 1px solid red; width: min-content; height: 100%;">
<img src="https://via.placeholder.com/120.png" style="max-height: 120px; height: 100%;" />
</div>
<div id="imagecontainer" style="position: relative; flex-shrink: 1; flex-basis: 150px; min-height: 0; box-sizing: border-box; border: 1px solid red; width: fit-content; height: 100%;">
<img src="https://via.placeholder.com/150.png" style="max-height: 150px; height: 100%;" />
</div>
</div>
Original image<br />
<img src="https://via.placeholder.com/150.png" style="border: 1px solid black;" />
<br />
Image in just a simple div with limited height
<div style="position: relative; height: 110px; border: 1px solid red; width: min-content;">
<img src="https://via.placeholder.com/150.png" style="max-height: 150px; height: 100%;" />
</div>
Edit Solution:
Based on the accepted answer the solution for the example above is this (calculating height percentages based on the values that would otherwise be the flex-basis):
Image in flex container with limited height
<div style="border: 1px solid black; height: 110px; width: 200px;">
<div id="imagecontainer" style="box-sizing: border-box; border: 1px solid red;height: calc(100% / (270 / 120)); width: max-content;">
<img src="https://via.placeholder.com/120.png" style="height: 100%;" />
</div>
<div id="imagecontainer" style="box-sizing: border-box; border: 1px solid red;height: calc(100% / (270 / 150)); width: max-content;">
<img src="https://via.placeholder.com/150.png" style="height: 100%;" />
</div>
</div>
Note that this doesn't require flexbox anymore.