我自己做的flexbox遇到了问题。
它里面有一些图片(每个图片都在它们自己的DIV元素中,但这不是必要的),一旦它们开始包装,它们就不会缩放到容器的高度并溢出……隐藏溢出不是解决方案,因为图像需要完全可见。
我让它们在一行中缩放,这很好,但是因为它们太小了,你看不到任何东西,我想让它们包装,但仍然缩放(保持图像比例)。
body, html {
height: 100%;
margin: 0;
padding: 0;
width: 300px;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
height: 100px;
background: black;
}
.box {
flex: 1 1 auto;
display: flex;
justify-content: center;
align-items: stretch;
}
.box img {
width: 100%;
object-fit: contain;
}<div class="container">
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
<div class="box">
<img src="http://placehold.it/80x80" alt="">
</div>
</div>
我正在寻找的解决方案是,一旦图像开始包装,它们的大小就会缩放,这样它们就会形成2行缩小的图像。
有人能告诉我我的大脑在这个过程中卡在哪里了吗?我尝试了许多不同的配置,使用了flexbox属性,比如收缩、基础等等。
问候Alkahna
发布于 2018-01-06 16:17:55
您可以考虑将图像添加为背景,并调整背景属性以保持缩放比例:
body,
html {
height: 100%;
margin: 0;
padding: 0;
width: 400px;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
height: 100px;
background: black;
}
.box {
flex: 1 1 auto;
display: flex;
justify-content: center;
align-items: stretch;
background-size:contain;
background-position:center;
background-repeat:no-repeat;
}
.box img {
width: 100%;
object-fit: contain;
}<div class="container">
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
<div class="box" style="background-image:url(http://placehold.it/80x80)">
</div>
</div>
https://stackoverflow.com/questions/48123401
复制相似问题