If I have a flexbox with a max-width: 500px
, and inside I have 3 img
elements, they extend beyond the max-width
of the flexbox.
However, if I wrap all of those img
elements in a div
, they remain contained within the max-width
of the flexbox.
I thought it might be due to the display
property, but changing the img
element to display: block
also doesn't keep it contained.
Overflow of flexbox:
.flex-box {
display: flex;
justify-content: center;
align-items: center;
max-width: 500px;
margin: 0 auto;
}
img {
width: 100%;
max-width: 100%;
}
<div class="flex-box">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
Contained within flexbox:
.flex-box {
display: flex;
justify-content: center;
align-items: center;
max-width: 500px;
margin: 0 auto;
}
img {
width: 100%;
max-width: 100%;
}
<div class="flex-box">
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
</div>