Most of images with intrinsic dimensions, that is a natural size, like a jpeg
image. If the specified size defines one of both the width and the height, the missing value is determined using the intrinsic ratio... - see MDN.
But that doesn't work as expected if the images that are being set as direct flex items with the current Flexible Box Layout Module Level 1, as far as I know.
See these discussions and bug reports might be related:
- Flexbugs #14 - Chrome/Flexbox Intrinsic Sizing not implemented correctly.
- Firefox Bug 972595 - Flex containers should use "flex-basis" instead of "width" for computing intrinsic widths of flex items
- Chromium Issue 249112 - In Flexbox, allow intrinsic aspect ratios to inform the main-size calculation.
As a workaround, you could wrap each <img>
with a <div>
or a <span>
, or so.
jsFiddle
.slider {
display: flex;
}
.slider>div {
min-width: 0; /* why? see below. */
}
.slider>div>img {
max-width: 100%;
height: auto;
}
<div class="slider">
<div><img src="https://picsum.photos/400/300?image=0" /></div>
<div><img src="https://picsum.photos/400/300?image=1" /></div>
<div><img src="https://picsum.photos/400/300?image=2" /></div>
<div><img src="https://picsum.photos/400/300?image=3" /></div>
</div>
4.5 Implied Minimum Size of Flex Items
To provide a more reasonable default minimum size for flex items,
this specification introduces a new auto value as the initial
value of the min-width and min-height properties defined in
CSS 2.1.
Alternatively, you can use CSS table
layout instead, which you'll get similar results as flexbox
, it will work on more browsers, even for IE8.
jsFiddle
.slider {
display: table;
width: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.slider>div {
display: table-cell;
vertical-align: top;
}
.slider>div>img {
max-width: 100%;
height: auto;
}
<div class="slider">
<div><img src="https://picsum.photos/400/300?image=0" /></div>
<div><img src="https://picsum.photos/400/300?image=1" /></div>
<div><img src="https://picsum.photos/400/300?image=2" /></div>
<div><img src="https://picsum.photos/400/300?image=3" /></div>
</div>
<div>
with the CSSbackground-image: url(...);background-size:contain; background-repeat:no-repeat
– Blazemonger