An initial setting of a flex container is align-items: stretch
.
That means that flex items will expand across the cross axis of the container.
In a row-direction container, like in the question, the cross axis is vertical.
That means the items (images, in this case) will cover the full height of the container.
However, when a flex item has a defined cross size, that overrides the stretch
default.
From the spec:
8.3. Cross-axis Alignment: the align-items
and align-self
properties
Flex items can be aligned in the cross axis of the current line of the
flex container, similar to justify-content
but in the perpendicular
direction.
stretch
If the cross size property of the flex item computes to auto
, and
neither of the cross-axis margins are auto
, the flex item is
stretched.
This is the key language:
If the cross size property of the flex item computes to auto
And this is how the spec defines "cross size property":
The width or height of a flex item, whichever is in the cross
dimension, is the item’s cross size. The cross size property is
whichever of width
or height
that is in the cross dimension.
https://www.w3.org/TR/css-flexbox-1/#cross-size-property
So your code appears to be playing out as defined in the spec.
This is what you have:
.flex-parent {
display: flex;
max-height: 10vh;
}
<div class="flex-parent">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Red_eyed_tree_frog_edit2.jpg/320px-Red_eyed_tree_frog_edit2.jpg">
<img width="320" height="240" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Red_eyed_tree_frog_edit2.jpg/320px-Red_eyed_tree_frog_edit2.jpg">
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…