我有一个DIV,使用CSS flexbox缩放到可用高度。在这个DIV中,我想在两个维度中与DIV一起缩放图像。这意味着它应该按比例保持其纵横比,并且小于相应DIV尺寸的尺寸应该居中。Flexbox图像缩放到高度并保持纵横比
我可以使图像遵循DIV的宽度,但不是高度。因此,肖像图像从DIV界限中逃脱。
这是一个jsFiddle来演示问题。
html, body {
height: 100%;
margin: 0;
}
.container {
display: flex;
flex-direction: column;
height: 100%;
}
.box {
flex: 1 1 auto;
display: flex;
justify-content: center;
align-items: center;
}
.box img {
width: auto;
height: auto;
max-width: 90%;
max-height: 90%;
}
<div class="container">
<div class="box"></div>
<div class="box" style="background: pink;">
<img src="//dummyimage.com/300" />
</div>
<div class="box"></div>
</div>
你想让图像适合高度,即使它自然较小? (在你的例子中你使用的是一张相当大的图片,所以我不确定) –
我想要实现的第一件事是它按需要缩小比例。 – languitar
不,对不起,图像应保留其原始长宽比。如果这个容器与容器的容器不同,则图像应该在调整大小后沿着小于容器的轴的中心。 – languitar