在 bootstrap 中,列之间有 space

columns in a row with space between them in bootstrap

我在一个页面中有重复的产品

  <div class="col-lg-12 col-md-12 col-sm-12 ">
            <div class="row">
                @foreach (var item in Model.Products)
                    {
                <div class="col-lg-3 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">
                    <section>
                        <div class="cart-title text-center w-100" >
                            <a href="~/products/detail/@item.Id">  @item.Title</a>
                        </div>
                        <div class="thumb">
                            <a href="~/products/detail/@item.Id" class="d-block">

                                <img src="~/@item.ImageSrc">
                            </a>
                        </div>

                        <div class="cart-title">price: 
                            <span class="cart-title">
                                @item.Price.ToString("n0")
                            </span>
                        </div>
                    </section>
                </div>
                    }
            </div>
        </div>

它做了这样的东西

但是我希望每本书之间有边距,书的起止位置是货物,但是书的每一列之间应该有space。如果我设置

 <div class="col-lg-3 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">

to

它会变成

如何在每一行中保留 for,但在每一列之间保留 space?

这是 css:

.cart-col {
width: 100%;
padding: 10px;
border: 1px solid #e2efef;
box-shadow: 0 0 7px 0 rgba(0, 0, 0, 0.29);
position: relative;
display: inline-block;
background: #fff;
border-radius: 10px;
margin-bottom:15px;

}

默认情况下 bootstrap col 从左到右填充。 如果它没有出现,意味着你在 cart-col class 中定义了一些填充。 所以,试试这个->

<div class="col-lg-3 col-md-3 col-xs-12">
    <div class="cart-col">

    </div>
</div>

如果您需要 space,例如 margin-buttom,请将 gy-4 放在行 class 中。 希望这会奏效。

cart-col div 应该在 <div class="col-lg-3 col-md-3 col-xs-12 order-1 d-block mb-3">

里面

请将此代码替换为

<div class="col-lg-12 col-md-12 col-sm-12 ">
    <div class="row">
        @foreach (var item in Model.Products)
        {
        <div class="col-lg-3 col-md-3 col-xs-12 order-1 d-block mb-3">
            <div class="cart-col">
                <section>
                    <div class="cart-title text-center w-100">
                        <a href="~/products/detail/@item.Id"> @item.Title</a>
                    </div>
                    <div class="thumb">
                        <a href="~/products/detail/@item.Id" class="d-block">

                            <img src="~/@item.ImageSrc">
                        </a>
                    </div>

                    <div class="cart-title">price:
                        <span class="cart-title">
                            @item.Price.ToString("n0")
                        </span>
                    </div>
            </div>
            </section>
        </div>
        }
    </div>
</div>

flex 上使用 Bootstrap 中的 justify-content-between class。所以只需将它添加为 class,在 row 上添加 div。我还会将 col-lg-3 更改为 col-lg-2,这样 four-row 项目就不会被迫占据全宽。

.cart-col {
  width: 100%;
  padding: 10px;
  border: 1px solid #e2efef;
  box-shadow: 0 0 7px 0 rgba(0, 0, 0, 0.29);
  position: relative;
  display: inline-block;
  background: #fff;
  border-radius: 10px;
  margin-bottom: 15px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

<div class="col-lg-12 col-md-12 col-sm-12 ">
  <div class="row justify-content-between">
    <div class="col-lg-2 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">
      <section>
        <div class="cart-title text-center w-100">
          <a href="~/products/detail/@item.Id">  @item.Title</a>
        </div>
        <div class="thumb">
          <a href="~/products/detail/@item.Id" class="d-block">

            <img src="~/@item.ImageSrc">
          </a>
        </div>

        <div class="cart-title">price:
          <span class="cart-title">
                                @item.Price.ToString("n0")
                            </span>
        </div>
      </section>
    </div>
    <div class="col-lg-2 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">
      <section>
        <div class="cart-title text-center w-100">
          <a href="~/products/detail/@item.Id">  @item.Title</a>
        </div>
        <div class="thumb">
          <a href="~/products/detail/@item.Id" class="d-block">

            <img src="~/@item.ImageSrc">
          </a>
        </div>

        <div class="cart-title">price:
          <span class="cart-title">
                                @item.Price.ToString("n0")
                            </span>
        </div>
      </section>
    </div>
    <div class="col-lg-2 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">
      <section>
        <div class="cart-title text-center w-100">
          <a href="~/products/detail/@item.Id">  @item.Title</a>
        </div>
        <div class="thumb">
          <a href="~/products/detail/@item.Id" class="d-block">

            <img src="~/@item.ImageSrc">
          </a>
        </div>

        <div class="cart-title">price:
          <span class="cart-title">
                                @item.Price.ToString("n0")
                            </span>
        </div>
      </section>
    </div>
    <div class="col-lg-2 col-md-3 col-xs-12 order-1 d-block mb-3 cart-col " style="padding-left:10px;">
      <section>
        <div class="cart-title text-center w-100">
          <a href="~/products/detail/@item.Id">  @item.Title</a>
        </div>
        <div class="thumb">
          <a href="~/products/detail/@item.Id" class="d-block">

            <img src="~/@item.ImageSrc">
          </a>
        </div>

        <div class="cart-title">price:
          <span class="cart-title">
                                @item.Price.ToString("n0")
                            </span>
        </div>
      </section>
    </div>
  </div>
</div>