我有一个随机宽度图像的flexbox网格:
我想将每行的元素拉伸到全宽以消除空白区域 - 如何使用CSS实现这一点?
justify-content
似乎没什么帮助。我已经阅读了一些JavaScript技术但我希望尽可能避免使用它。
[已编辑]预期结果:example
.grid {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.grid .grid-item {
width: auto;
display: inline-block;
vertical-align: bottom;
float: none;
margin: 1px;
overflow: hidden;
}
.grid .grid-item .grid-in {
padding: 0;
width: auto;
height: 190px;
vertical-align: bottom;
}
.grid img {
max-width: 100%;
width: 100%;
height: auto;
padding: 0;
}
<div class="grid">
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
</div>
答案 0 :(得分:0)
首先包装容器中的每一行,例如.grid-row
,它将具有您之前分配给.grid
的那些Flexbox属性。
现在为每个单元格设置flex-grow
属性,只要每行值的比率正确,该值就可以是任何值。您可以简单地将其设置为单元格的宽度(没有单位),因此第一个单元格将具有flex-grow: 190
。此外,请务必在flex-shrink
上设置1
至.grid-item
以避免this flexbug。
要使图片一直延伸,请从inline-block
中移除所有.grid-in
商家。要使它们按比例增加高度,而不是裁剪为190px
,请从width: 190px
移除.grid-in
。
最后,要将它们分开,在这种情况下,最好在padding: 1px
上使用.grid-item
而不是margin: 1px
上的.grid-in
。
由于演示没有足够的减少,我为了清楚起见清理了它。希望您知道如何在原始代码中实现它。
.grid-row {
display: flex;
}
.grid-item {
flex-shrink: 1;
padding: 1px;
}
.grid-row:nth-child(1) .grid-item:nth-child(1) { flex-grow: 190; }
.grid-row:nth-child(1) .grid-item:nth-child(2) { flex-grow: 250; }
.grid-row:nth-child(1) .grid-item:nth-child(3) { flex-grow: 100; }
.grid-row:nth-child(2) .grid-item:nth-child(1) { flex-grow: 190; }
.grid-row:nth-child(2) .grid-item:nth-child(2) { flex-grow: 250; }
.grid-row:nth-child(2) .grid-item:nth-child(3) { flex-grow: 190; }
.grid-row:nth-child(3) .grid-item:nth-child(1) { flex-grow: 100; }
.grid-row:nth-child(3) .grid-item:nth-child(2) { flex-grow: 190; }
.grid-row:nth-child(3) .grid-item:nth-child(3) { flex-grow: 250; }
.grid-row:nth-child(4) .grid-item:nth-child(1) { flex-grow: 190; }
.grid-row:nth-child(4) .grid-item:nth-child(2) { flex-grow: 100; }
.grid-row:nth-child(4) .grid-item:nth-child(3) { flex-grow: 190; }
.grid img {
width: 100%;
height: auto;
vertical-align: middle;
}
&#13;
<div class="grid">
<div class="grid-row">
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
</div>
<div class="grid-row">
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
</div>
<div class="grid-row">
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/250x190" alt="">
</div>
</div>
</div>
<div class="grid-row">
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/100x190" alt="">
</div>
</div>
<div class="grid-item">
<div class="grid-in">
<img src="http://placehold.it/190x190" alt="">
</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:0)
我偶然发现了这个问题,遗憾的是无法使用Flexbox解决它。
答案可能会让你感到惊讶,但我发现这对我来说是一个合适的解决方案是一张桌子,是的,你听到我说对了,<table>
!
要记住一些限制因素:
table {
width: 100%;
margin: 0 0 10px 0;
padding: 0;
border-collapse: collapse;
}
table td {
padding: 0;
margin: 0;
}
img {
display: block;
width: 100%;
max-width: 100%;
}
<table>
<tr>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
<td>
<img src="http://placehold.it/250x190" alt="">
</td>
<td>
<img src="http://placehold.it/100x190" alt="">
</td>
</tr>
</table>
<table>
<tr>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
<td>
<img src="http://placehold.it/250x190" alt="">
</td>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
</tr>
</table>
<table>
<tr>
<td>
<img src="http://placehold.it/100x190" alt="">
</td>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
<td>
<img src="http://placehold.it/250x190" alt="">
</td>
</tr>
</table>
<table>
<tr>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
<td>
<img src="http://placehold.it/100x190" alt="">
</td>
<td>
<img src="http://placehold.it/190x190" alt="">
</td>
</tr>
</table>
您还可以使用其他html元素并使用css来模仿表行为
display: table | table-row | table-cell