我有包含图像部分和文本部分的弹性框项目,两者都有50%的宽度。我希望项目的高度受图像高度和图像的高度限制,以保持其宽高比。
正如你在代码笔中看到的那样:
HTML
<div class="flex-container">
<div class="inner-flex-container">
<img
src="https://forums.macrumors.com/attachments/img_0264-png.670820/"
class="image" />
<div class="text">Text</div>
</div>
<div class="inner-flex-container">
<img
src="https://forums.macrumors.com/attachments/img_0264-png.670820/"
class="image" />
<div class="text">Text</div>
</div>
<div class="inner-flex-container">
<img
src="https://forums.macrumors.com/attachments/img_0264-png.670820/"
class="image" />
<div class="text">Text</div>
</div>
<div class="inner-flex-container">
<img
src="https://forums.macrumors.com/attachments/img_0264-png.670820/"
class="image" />
<div class="text">Text</div>
</div>
</div>
CSS
.flex-container {
width: 600px;
background-color: gray;
margin-left: auto;
margin-right: auto;
flex-wrap: wrap;
display: flex;
}
.inner-flex-container {
width: 50%;
}
.image, .text {
display: inline-block
}
.image {
height: auto;
max-width: 50%
}
代码笔