These are initial settings in a flex container:
flex-grow: 0
flex-shrink: 1
flex-basis: auto
The shorthand would be:
Therefore, even though you haven't specified these rules in your code, they apply to the images.
The images cannot grow, they can shrink (equally and just enough to avoid overflowing the container), and they are initially sized to their natural width (400px).
This is what you're seeing in Firefox. The images are shrinking to fit nicely within the container.
In Firefox, flex rules are overriding the natural dimensions of the image.
In Chrome, however, the reverse is true. The dimensions of the images are prevailing.
The simple cross-browser solution is to wrap the images in another element, so this new wrapper becomes the flex item and takes on the default flex: 0 1 auto
, and nothing needs to be overridden.
img {
width: 100%;
}
<h1>Window width:</h1>
<div style="display: flex">
<span><img src="https://unsplash.it/400/225?image=10" alt="1"></span>
<span><img src="https://unsplash.it/400/225?image=11" alt="2"></span>
<span><img src="https://unsplash.it/400/225?image=12" alt="3"></span>
</div>
<h1>Wrapped in 500px wide div:</h1>
<div style="width: 500px; overflow: auto">
<div style="display: flex">
<span><img src="https://unsplash.it/400/225?image=10" alt="1"></span>
<span><img src="https://unsplash.it/400/225?image=11" alt="2"></span>
<span><img src="https://unsplash.it/400/225?image=12" alt="3"></span>
</div>
</div>
In terms of which browser is adhering to spec guidance, it appears that would be Firefox. In a flex container, flex rules should prevail:
7.1. The flex
Shorthand
When a box is a flex item, flex
is consulted instead of the main size property to determine the main size of the box.
The flex item’s main size property is either the width
or height
property.
I say Firefox "appears" to be correct because the spec is saying that flex rules should prevail over the CSS width
and height
properties.
Of course, the dimensions of the images in this case are not defined in CSS. They are the natural dimensions of the image. So this scenario may be left open for interpretation, and Chrome may not be violating any guidelines.
However, in another scenario, where the height
property was a factor, Firefox stuck with flex
, while Chrome went with height
: Why is Firefox not honoring flexed div's height, but Chrome is?