要设置flexbox项目之间的最小距离,我使用margin: 0 5px on .item和margin: 0 -5px on container。对我来说,这似乎是一种hack,但我找不到更好的方法来做到这一点。

#箱{ 显示:flex; 宽度:100 px; Margin: 0 -5px; } .item { 背景:灰色; 宽度:50 px; 高度:50 px; 边距:0 5px; } < div id =“盒子”> < div class = '物品' > < / div > < div class = '物品' > < / div > < div class = '物品' > < / div > < div class = '物品' > < / div > < / div >


当前回答

在我的解决方案中使用Flexbox,我为父元素(容器)使用了justify-content属性,并在项目的flex-basis属性中指定了边界。 检查下面的代码片段:

.container { 显示:flex; Flex-flow:行换行; justify-content:空间; margin-bottom: 10 px; } .item { 高度:50 px; 显示:flex; justify-content:中心; 对齐项目:中心; background - color: # 999; } .item-1-4 { 弹性基础:calc(25% - 10px); } .item-1-3 { flex- base: calc(33.33333% - 10px); } .item-1-2 { Flex-basis: calc(50% - 10px); } < div class = "容器" > <div class="item item-1-4">1</div> . <div class="item item-1-4">2</div> . <div class="item item-1-4">3</div> . <div class="item item-1-4">4</div> . < / div > < div class = "容器" > <div class="item item-1-3">1</div> . <div class="item item-1-3">2</div> . <div class="item item-1-3">3</div> . < / div > < div class = "容器" > <div class="item item-1-2">1</div> . <div class="item item-1-2">2</div> < / div >

其他回答

在我的解决方案中使用Flexbox,我为父元素(容器)使用了justify-content属性,并在项目的flex-basis属性中指定了边界。 检查下面的代码片段:

.container { 显示:flex; Flex-flow:行换行; justify-content:空间; margin-bottom: 10 px; } .item { 高度:50 px; 显示:flex; justify-content:中心; 对齐项目:中心; background - color: # 999; } .item-1-4 { 弹性基础:calc(25% - 10px); } .item-1-3 { flex- base: calc(33.33333% - 10px); } .item-1-2 { Flex-basis: calc(50% - 10px); } < div class = "容器" > <div class="item item-1-4">1</div> . <div class="item item-1-4">2</div> . <div class="item item-1-4">3</div> . <div class="item item-1-4">4</div> . < / div > < div class = "容器" > <div class="item item-1-3">1</div> . <div class="item item-1-3">2</div> . <div class="item item-1-3">3</div> . < / div > < div class = "容器" > <div class="item item-1-2">1</div> . <div class="item item-1-2">2</div> < / div >

我已经找到了一个基于一般兄弟选择器~的解决方案,并允许无限嵌套。

有关工作示例,请参阅此代码笔

基本上,在列容器中,前面有另一个子容器的每一个子容器都有上边距。同样,在每个行容器中,前面有另一个子容器的子容器都有左边距。

.box { 显示:flex; flex-grow: 1; flex-shrink: 1; } .box。列{ flex-direction:行; } .box.columns > .box ~。盒子{ margin-left: 5 px; } .box。行{ flex-direction:列; } .box.rows > .box ~。盒子{ margin-top: 5 px; } <div class="box columns"> < span style=" font - family:宋体;"> < / div > <div class="box rows"> <div class="box rows"> < span style=" font - family:宋体;"> < / div > < span style=" font - family:宋体;"> < / div > <div class="box columns"> < span style=" font - family:宋体;"> < / div > < span style=" font - family:宋体;"> < / div > < / div > < / div > < span style=" font - family:宋体;"> < / div > < / div > < / div >

你可以用下面的方程

.container { max-width: 960 px; 保证金:0自动; 填充:4rem 0; } .flex { 显示:flex; 对齐项目:中心; justify-content:之间的空间; flex-wrap:包装; } .flex:{后 内容:“”; max-width:钙(100% * var(——坳)/ 12 - var(差距)); 宽度:100%; } @media (max-width: 960px) { .flex:{后 max-width: calc(100% * var(——colTablet) / 12 - var(——gap)); } } @media (max-width: 680px) { .flex:{后 max-width: calc(100% * var(——colMobile) / 12 - var(——gap)); } } .flex .item { max-width:钙(100% * var(——坳)/ 12 - var(差距)); 宽度:100%; } @media (max-width: 960px) { .flex .item { max-width: calc(100% * var(——colTablet) / 12 - var(——gap)); margin-bottom: 1快速眼动; } .flex .item:last child { margin-bottom:设置; } } @media (max-width: 680px) { .flex .item { max-width: calc(100% * var(——colMobile) / 12); } } .flex .item .card { 背景:# eee; text-align:中心; 填充:2快速眼动; } <div class="flex container" style="——col: 3;——colTablet: 6;——colMobile: 12个;——差距:2%”> <div class="item" style="——font - family:宋体;——colTablet: 6;——colMobile: 12个;——差距:2%”> < div class = "牌”> < h2 > Hello world < / h2 > < / div > < / div > <div class="item" style="——font - family:宋体;——colTablet: 6;——colMobile: 12个;——差距:2%”> < div class = "牌”> < h2 > Hello world < / h2 > < / div > < / div > <div class="item" style="——font - family:宋体;——colTablet: 6;——colMobile: 12个;——差距:2%”> < div class = "牌”> < h2 > Hello world < / h2 > < / div > < / div > < / div >

根据#ChromeDevSummit的说法,在Firefox和基于chrome的浏览器中,Flexbox有一个gap属性的实现。

这是一个现场演示

我已将此方法用于包装和固定宽度的列。这里的关键是calc()

SCSS样本

$gap: 10px;

dl {
  display: flex;
  flex-wrap: wrap;
  padding: $gap/2;

  dt, dd {
    margin: $gap/2;}

  dt { // full width, acts as header
    flex: 0 0 calc(100% - #{$gap});}

  dd { // default grid: four columns 
    flex: 0 0 calc(25% - #{$gap});}

  .half { // hall width columns
    flex: 0 0 calc(50% - #{$gap});}

}

完整的Codepen样本