I'm wondering why wrapping img
flex items in div
s will cause them to scale correctly but img
items alone do not.
If you check out this JSFiddle, you'll see that wrapping an image in a div does work as suggested by other posts on StackOverflow, but it doesn't seem to work otherwise. And align-self
doesn't change anything as suggested here. I tried to align-items
so that it would override the default stretch
but that doesn't work. I also tried clearing the min-height
and min-width
values since those are by default set to auto
. This answer justifies using the div
wrapper except it doesn't explain why it works for div
flexbox items and not for img
items directly.
Here's some HTML:
.stack {
display:flex;
max-width:600px;
align-items:center; /* this does not cause image tags to resize */
}
.stack img {
min-height:0;
min-width:0;
height:auto;
align-self:center;
}
.test img {
width:100%;
}
<div class="stack">
<div class="test">
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<!--Wrapping in a div works-->
</div>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
</div>
Why is seemingly the only solution to wrap the image with a div? I don't understand why the only way to scale the image correctly is through a div
wrapper.