html - 在网格中使用 flexbox 时在 div 之间添加空间

标签 html css flexbox

我正在使用 flexbox 创建一个网格,并希望在每个 div 之间添加空间。例如:

enter image description here

但我不希望额外的空间出现在右侧和底部,因为这样结束的 div 就不会均匀地出现在容器中,我只需要 div 之间的间距。然后在调整大小时,这应该发生:

enter image description here

是否可以使用 flexbox 来实现?

我目前的尝试:

#feature-box-container {
  display: inline-flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 30px;
}

.feature-box {
  display: inline-flex;
  flex-grow: 1;
  width: 300px;
  padding: 12px;
  color: grey;
  border: 1px solid #4F5B93;
  border-radius: 8px;
}

.feature-box img {
  height: 48px;
  width: auto;
  margin-right: 18px;
}

.feature-box p {
  word-wrap: break-word;
  margin: 0;
  display: flex;
  justify-content: center;
  flex-direction: column;
}
<div id="feature-box-container">

  <div class="feature-box">

    <img src="images/router-icon.png">

    <p>Something would go here. Something would go here. Something would go here.</div>

  <div class="feature-box">

    <img src="images/template-icon.png">

    <p>Something would go here. Something would go here. </p>

  </div>

  <div class="feature-box">

    <img src="images/security-icon.png">

    <p>Something would go here. Something would go here. Something would go here. Something would go here. </p>

  </div>

  <div class="feature-box">

    <img src="images/ssl-icon.png">

    <p>Something would go here. Something would go here. </p>

  </div>

  <div class="feature-box">

    <img src="images/data-access-icon.png">

    <p>Something would go here. Something would go here. Something would go here. </p>

  </div>

  <div class="feature-box">

    <img src="images/notification-icon.png">

    <p>Support for notifications and email delivery.</p>

  </div>

</div>

最佳答案

feature-box 上添加边距 margin: 0 5px 5px;

.feature-box
{
    display: inline-flex;
    flex-grow: 1;
    width: 300px;
    padding: 12px;
    margin: 0 5px 5px;
    color: grey;
    border: 1px solid #4F5B93;
    border-radius: 8px;
}

关于html - 在网格中使用 flexbox 时在 div 之间添加空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57710358/

相关文章:

html - 停止使用最大宽度 : 100% 调整图像大小

html - 无需使用表格即可调整列高

html - 如何选择两个 <div> 标签中的图像?

html - 0 < lineWidth < 1 时的 Canvas 线条行为

css - 响应式图像位于图像之上

html - 为什么我的导航栏不能像纵向那样滚动?

CSS3 Flexbox : display: box vs. flexbox 与 flex

reactjs - 如何在react-native中使用flexbox来制作这个 View

html - 为什么 flex 元素不缩小过去的内容大小?

html - MaterializeCSS 中的多项选择?