【问题标题】:Trying to add white space vertically between cols in a row - Bootstrap 4尝试在一行中的列之间垂直添加空格 - Bootstrap 4
【发布时间】:2017-09-10 19:55:39
【问题描述】:

我在这里遇到了一个问题。我连续 8 列中的每一列都使用了背景图像,现在我需要在它们之间添加空白。我在这里创建了两个类,它们应该可以工作,但是出了点问题。另外,我在过去的项目中使用过这个,并且在 row 元素中使用了thin-gutters 和 waffle,但现在 waffle 仅适用于 col 的子元素。

这是我的代码...

<div id="products">
<div class="container-fluid">
    <div class="row thin-gutters text-center">
        <div class="col-12 col-md-6 col-xl-3 product-images waffle ceramic-tile">
            <h2 class="product-title"><a href="#">Ceramic Tile</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle carpet">
            <h2 class="product-title"><a href="#"> carpet</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle hardwood">
            <h2 class="product-title"><a href="#">hardwood</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle countertops">
            <h2 class="product-title"><a href="#">countertops</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle laminate">
            <h2 class="product-title"><a href="#">laminate</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle stone-tile">
            <h2 class="product-title"><a href="#">stone tile</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle vinyl">
            <h2 class="product-title"><a href="#">vinyl</a></h2>
        </div>
        <div class="col-12 col-md-6 col-xl-3 product-images waffle area-rugs">
            <h2 class="product-title"><a href="#">area rugs</a></h2>
        </div>
    </div>
</div>

还有我的 CSS...

.thin-gutters {
        margin-right: -2px;
        margin-left: -2px;
    }

    @media (min-width: 576px) {
        .thin-gutters {
            margin-right: -2px;
            margin-left: -2px;
        }
    }

    @media (min-width: 768px) {
        .thin-gutters {
            margin-right: -2px;
            margin-left: -2px;
        }
    }

    @media (min-width: 992px) {
        .thin-gutters {
            margin-right: -2px;
            margin-left: -2px;
        }
    }

    @media (min-width: 1200px) {
        .thin-gutters {
            margin-right: -2px;
            margin-left: -2px;
        }
    }

    .thin-gutters > .col,
    .thin-gutters > [class*="col-"] {
        padding-right: 2px;
        padding-left: 2px;
    }

    .waffle {
        margin-bottom: 4px;
    }

    .waffle > .col,
    .waffle > [class*="col-"] {
        padding-bottom: 4px;
    }

【问题讨论】:

  • .waffle &gt; .col, .waffle &gt; [class*="col-"] { padding-bottom: 4px; } 的原因是什么.. 它什么也不做,因为waffle 内没有col
  • 这就是我所说的,在我处理的上一个项目中,它在父元素(行)上工作,但这次它只影响 col 如果它在那个元素中。不知道为什么要改,代码完全一样。
  • 但是 HTML 结构不是......华夫饼内没有 col 那么你希望填充如何工作?

标签: multiple-columns spacing bootstrap-4


【解决方案1】:

在列中添加margin-bottom..

.thin-gutters > .col,
.thin-gutters > [class*="col-"] {
    padding-right: 2px;
    padding-left: 2px;
    margin-bottom: 10px;
}

http://www.codeply.com/go/N9snQAKA4K

--

更新 Bootstrap 4 测试版

现在spacing utils 可用于边距和填充。例如,

mb-2 用于 2 个间距单位的边距底部。

【讨论】: