对齐多个flexbox行的列

时间:2017-01-21 21:05:55

标签: html css css3 flexbox

我使用flexbox作为网格列

在第一行中,我有三个dr-col-1和两个dr-col-2(共有flex-grow个步骤。)

在第二行,我有七个dr-col-1

当我渲染它时,我注意到它的对齐不精确:

enter image description here

如何精确对齐两行中的列?



.dr-row {
  display: flex;
}
.dr-col-1 {
  flex-grow: 1;
  flex-basis: 0;
  border: 1px solid black;
}

.dr-col-2 {
  flex-grow: 2;
  flex-basis: 0;
  border: 1px solid black;
}

<div class="dr-row">
    <div class="dr-col-2">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-2">
      five
    </div>
  </div>

<div class="dr-row">
    <div class="dr-col-1">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-1">
      five
    </div>
    <div class="dr-col-1">
      six
    </div>
    <div class="dr-col-1">
      seven
    </div>
  </div>
&#13;
&#13;
&#13;

Codepen

4 个答案:

答案 0 :(得分:2)

当你有两个.dr-col-1时,有4个边框,4 * 1px = 4px。

当你有一个.dr-col-2时,有2个边框,2 * 1px = 2px。

所以你需要补偿2px的差异,例如通过增加flex-basis

.dr-col-2 {
  flex-basis: 2px; /* flexFactor * borderWidth */
}

&#13;
&#13;
.dr-row {
  display: flex;
}
.dr-col-1 {
  flex-grow: 1;
  flex-basis: 0;
  border: 1px solid black;
}
.dr-col-2 {
  flex-grow: 2;
  flex-basis: 2px;
  border: 1px solid black;
}
&#13;
<div class="dr-row">
  <div class="dr-col-2">one</div>
  <div class="dr-col-1">two</div>
  <div class="dr-col-1">three</div>
  <div class="dr-col-1">four</div>
  <div class="dr-col-2">five</div>
</div>
<div class="dr-row">
  <div class="dr-col-1">one</div>
  <div class="dr-col-1">two</div>
  <div class="dr-col-1">three</div>
  <div class="dr-col-1">four</div>
  <div class="dr-col-1">five</div>
  <div class="dr-col-1">six</div>
  <div class="dr-col-1">seven</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

flex-grow属性旨在分配容器中的可用空间。它不适用于灵活项目的特定大小(flex-basis)。

列没有对齐,因为第二行中有更多垂直边框。

更具体地说,第一行有8px的边框宽度,第二行有12px的边框宽度(不包括边缘)。

这意味着第一行中有更多的可用空间,这会导致错位。

而不是使用flex-grow尝试使用flex-basis

&#13;
&#13;
.dr-row {
  display: flex;
}
.dr-col-1 {
  flex: 0 0 14.29%;
  border: 1px solid black;
  box-sizing: border-box;
}

.dr-col-2 {
  flex: 0 0 28.58%;
  border: 1px solid black;
  box-sizing: border-box;
}
body { margin: 0; }
&#13;
<div class="dr-row">
    <div class="dr-col-2">one</div>
    <div class="dr-col-1">two</div>
    <div class="dr-col-1">three</div>
    <div class="dr-col-1">four</div>
    <div class="dr-col-2">five</div>
  </div>

<div class="dr-row">
    <div class="dr-col-1">one</div>
    <div class="dr-col-1">two</div>
    <div class="dr-col-1">three</div>
    <div class="dr-col-1">four</div>
    <div class="dr-col-1">five</div>
    <div class="dr-col-1">six</div>
    <div class="dr-col-1">seven</div>
  </div>
&#13;
&#13;
&#13;

revised codepen

有关详细信息,请参阅: flex-grow not sizing flex items as expected

答案 2 :(得分:1)

边界也需要一些地方

这里是相同的HTML / CSS,但我使用的是轮廓而不是边框​​。

https://jsfiddle.net/nfo1pg9f/

.dr-row {
  display: flex;
  box-sizing: border-box;
}
.dr-col-1 {
  flex-grow: 1;
  flex-basis: 0;
  outline: 1px solid black;
}

.dr-col-2 {
  flex-grow: 2;
  flex-basis: 0;
  outline: 1px solid black;
}
<div class="dr-row">
    <div class="dr-col-2">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-2">
      five
    </div>
  </div>

<div class="dr-row">
    <div class="dr-col-1">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-1">
      five
    </div>
    <div class="dr-col-1">
      six
    </div>
    <div class="dr-col-1">
      seven
    </div>
  </div>

答案 3 :(得分:0)

这里尝试一下,不确定它是否是最佳解决方案,但无论浏览器大小如何,它似乎都完美对齐

边框似乎在元素上关闭,向grow增加一个像素:2;似乎再次重新调整事物。我为外观目的添加了一些边距和填充

&#13;
&#13;
.dr-row {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  
}
.dr-row div{
  margin:5px;
  flex-basis: 0;
  border: 1px solid black;
}
.dr-col-1 {
  flex-grow: 1;
}

.dr-col-2 {
  flex-grow: 2;
  padding: 6px;
}
&#13;
<div class="dr-row">
    <div class="dr-col-2">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-2">
      five
    </div>
  </div>

<div class="dr-row">
    <div class="dr-col-1">
      one
    </div>

    <div class="dr-col-1">
      two
    </div>

    <div class="dr-col-1">
      three
    </div>

    <div class="dr-col-1">
      four
    </div>

    <div class="dr-col-1">
      five
    </div>
    <div class="dr-col-1">
      six
    </div>
    <div class="dr-col-1">
      seven
    </div>
  </div>
&#13;
&#13;
&#13;