Flexbox:如何将行的元素拉伸到全宽?

时间:2015-07-28 08:39:34

标签: css flexbox

我有一个随机宽度图像的flexbox网格:

enter image description here

我想将每行的元素拉伸到全宽以消除空白区域 - 如何使用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>

2 个答案:

答案 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

由于演示没有足够的减少,我为了清楚起见清理了它。希望您知道如何在原始代码中实现它。

&#13;
&#13;
.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;
&#13;
&#13;

答案 1 :(得分:0)

我偶然发现了这个问题,遗憾的是无法使用Flexbox解决它。 答案可能会让你感到惊讶,但我发现这对我来说是一个合适的解决方案是一张桌子,是的,你听到我说对了,<table>

要记住一些限制因素:

  1. 您应该知道每行所需的单元格/列数。
  2. 如果列的每行都有不同的宽度,则每行需要一个表元素。 ('会员2000s?)
  3. 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