要设置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 >


当前回答

你可以利用新的房产缺口。我复制粘贴了我在本文中找到的解释,以及更多的信息

CSS的网格布局有差距(以前的网格差距)有一段时间了。通过指定包含元素的内部间距而不是子元素周围的间距,gap解决了许多常见的布局问题。例如,使用gap,你不必担心子元素的边距会在包含元素的边缘造成不必要的空白:

不幸的是,目前只有FireFox支持flex布局中的gap。

@use postcss-preset-env { 阶段:0; 浏览器:最近2个版本 } 节{ 宽度:30大众; 显示:网格; 差距:1快速眼动; Grid-template-columns: repeat(auto-fit, minmax(12ch, 1fr)); & (flex) { 显示:flex; flex-wrap:包装; } margin-bottom: 3快速眼动; } .tag { 颜色:白色; 背景:hsl(265 100% 47%); 填充:.5rem 1rem; border - radius: 1快速眼动; } 按钮{ 显示:inline-flex; 名:中心; 差距:.5rem; 背景:hsl(265 100% 47%); 边框:1px固体hsl(265 100% 67%); 颜色:白色; 填充:1rem 2rem; border - radius: 1快速眼动; 字体大小:1.25快速眼动; } 身体{ 最小高度:100 vh; 显示:flex; flex-direction:列; justify-content:中心; 对齐项目:中心; } < >节 <标题> < / h1 >网格 < div class = "标签" >的< / div > < div class = "标签" >首席运营官< / div > < div class = "标签" > Rad < / div > < div class = "标签" > < / div >数学 < / >节 < br > < flex节> <标题> Flex h1 > < / < div class = "标签" >的< / div > < div class = "标签" >首席运营官< / div > < div class = "标签" > Rad < / div > < div class = "标签" > < / div >数学 < / >节

其他回答

盒子容器上的负边距技巧效果很好。这是另一个例子,工作的秩序,包装和什么不。

.container { 边框:1px纯绿色; 宽度:200 px; 显示:inline-block; } #箱{ 显示:flex; flex-wrap: wrap-reverse; 保证金:-10 px; 边框:1px纯红色; } .item { Flex: 1台自动; 顺序:1; 背景:灰色; 宽度:50 px; 高度:50 px; 保证金:10 px; 边框:1px纯蓝色; } 当代{ 秩序:0; } < div class =容器> < div id =“盒子”> < div class = '物品' > 1 < / div > < div class = '物品' > < / div > 2 <div class='item first'>3*</div> 4 < div class = '物品' > < / div > < div class = '物品' > < / div > 5 < / 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;宽度:230 px;轮廓:1px纯蓝色;} .item{背景:灰色;宽度:50 px;身高:100 px;} /* ----- Flexbox gap: ----- */ #box > * + * { margin-left: 10 px; } < div id =“盒子”> < div class = '物品' > < / div > < div class = '物品' > < / div > < div class = '物品' > < / div > < div class = '物品' > < / div > < / div >

如果你需要支持伸缩包装,你可以使用wrapper元素:

.flex{显示:flex;flex-wrap:包装;} .box{背景:灰色;身高:100 px;min-width: 100 px;flex:汽车;} .flex包装{轮廓:1px固体红色;} /* -----伸缩间隙10px: ----- */ .flex > * { 保证金:5 px; } .flex { 保证金:5 px; } .flex-wrapper { 宽度:400 px;/* optional */ 溢出:隐藏;/* optional */ } < div class = ' flex-wrapper ' > < div class = ' flex ' > < div class = '盒子' > < / div > < div class = '盒子' > < / div > < div class = '盒子' > < / div > < div class = '盒子' > < / div > < div class = '盒子' > < / div > < / div > < / div >

这不是黑客。 bootstrap和它的网格也使用了同样的技术,不过,bootstrap为它的cols使用了padding而不是margin。

.row {
  margin:0 -15px;
}
.col-xx-xx {
  padding:0 15px;
}

做到这一点的简单方法是在children div中添加margin-left和margin-right,并相应地调整margin值

<div class="a">
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
</div>

css:

.a{
   display: flex;
   justify-content: center;
   background-color: black;
}

.b{
  height: 25px;
  width: 25px;
  background-color: grey;
  margin-left: 5px;
  margin-right: 5px;
}