【问题标题】:Bootstrap - How can I add space between cardsBootstrap - 如何在卡之间添加空间
【发布时间】:2021-12-05 16:53:27
【问题描述】:

我正在尝试在我的卡片之间放置空间。

我有 4 张卡并排在一起。但是当我在卡片 div 上使用mr-3 时,最后一张卡片在下面。我不希望这种情况发生。

如何让所有 4 张卡片之间有空格并在同一行/行中?

我也尝试了d-flex justify-content-around,但它不起作用。

下面是我的代码

<div class="container">
      <div class="row">
        <div class="card col-md-3">
          <img src="..." class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        <div class="card col-md-3">
          <img src="..." class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        <div class="card col-md-3">
          <img src="..." class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        <div class="card col-md-3">
          <img src="..." class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>
    </div>

第一张图片:

最后一张卡片在下面的第二张图片:

【问题讨论】:

标签: html bootstrap-4


【解决方案1】:

把卡片放在一个col...然后试试

cards,card-deck,card-colums 没有响应功能

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

要在两张卡片之间添加填充,最简单的方法是将mx-2 添加到每个卡片的 div 类中。

<div class="card col-md-3 mx-3" > <!--this mx-3 will ensure some space has to be given-->
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>

只需为每张卡片都这样做,它会腾出空间。

结果如下:-

最终代码:-

<div class="container">
    <div class="row">

      <div class="card col-md-3 mx-3" >
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
      <div class="card col-md-3 mx-3">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
      <div class="card col-md-3 mx-3">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
      <div class="card col-md-3 mx-3 my-2">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
    </div>
  </div>

其他来源:-

  1. https://getbootstrap.com/docs/4.0/utilities/spacing/
  2. https://www.youtube.com/watch?v=zWhBjLEhnbk

【讨论】:

  • 您好,感谢您的回答。我也这样做了,但我不希望最后一张卡实际上低于。我想让它们都在同一条线上。我该如何解决这个问题?