【问题标题】:Add spacing between columns in Bootstrap 4 grid在 Bootstrap 4 网格中添加列之间的间距
【发布时间】:2018-08-01 09:36:39
【问题描述】:

我有以下代码

<% include partials/header %>

<div class="container">

    <header class="jumbotron">
        <div class="container">
            <h1>Welcome to YelpCamp!</h1>

            <p>View our handpicked campgrounds from all over the world!</p>

            <p><a class="btn btn-primary btn-large" href="/campgrounds/new">Add New Campsite</a></p>
        </div>
    </header>

    <div class="row text-center" >
        <% campgrounds.forEach(function(campground){ %>
            <div class="col-lg-3 col-md-4 col-xs-6 img-thumbnail p-0 ">
                <img class="img-fluid" src="<%= campground.image %>">
                <caption> <%= campground.name %> </caption>
            </div>
        <% }) %>
    </div>

<a href="/">Back</a>
</div>


<% include partials/footer %>

我正在阅读露营地列表并将它们添加到 Bootstrap 网格中。 但是,元素之间没有间距。在 Bootstrap 3 中,这些间隙在使用缩略图时是自动的(在 4 中不可用)。 如果我添加一个边距mx-3,例如,

`class="col-lg-3 col-md-4 col-xs-6 img-thumbnail"`

这被添加到总宽度中,并且最小的项目被移位。 如何在列之间添加间距?

【问题讨论】:

  • 在图像上使用边距,为什么在列上有p-0?这将删除列之间的间距。
  • 使用 margin-left 或 margin-right 或 col-lg-offset-1

标签: css bootstrap-4 thumbnails


【解决方案1】:

Bootstrap 4 还可以为您提供自动间距。假设您使用正确的组件来满足您的需求。

在您的情况下,正确的组件是 figure 组件。将 figure-img img-thumbnail 类应用于图像。

对于图片说明,您应该使用这些(而不是您尝试使用的):

<figcaption class="figure-caption">Your Image Caption</figcaption>

如果您碰巧有奇数个缩略图或者您决定将col-md-4 添加到列中,那么行上的justify-content-center 类将使缩略图居中。

点击下方“运行代码sn-p”,展开整页进行测试:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container">
   <header class="jumbotron pl-5">
       <h1>Welcome to YelpCamp!</h1>
       <p>View our handpicked campgrounds from all over the world!</p>
       <p><a class="btn btn-primary btn-large" href="/campgrounds/new">Add New Campsite</a></p>
   </header>

   <div class="row justify-content-center text-center">
       <div class="col-6 col-lg-3">
           <figure class="figure">
               <img src="https://placeimg.com/800/400/animals" class="figure-img img-thumbnail">
               <figcaption class="figure-caption">A caption for the above image.</figcaption>
           </figure>
       </div>
       <div class="col-6 col-lg-3">
           <figure class="figure">
               <img src="https://placeimg.com/800/400/people" class="figure-img img-thumbnail">
               <figcaption class="figure-caption">A caption for the above image.</figcaption>
           </figure>
       </div>
       <div class="col-6 col-lg-3">
           <figure class="figure">
               <img src="https://placeimg.com/800/400/tech" class="figure-img img-thumbnail">
               <figcaption class="figure-caption">A caption for the above image.</figcaption>
           </figure>
       </div>
       <div class="col-6 col-lg-3">
           <figure class="figure">
               <img src="https://placeimg.com/800/400/arch" class="figure-img img-thumbnail">
               <figcaption class="figure-caption">A caption for the above image.</figcaption>
           </figure>
       </div>
   </div>
</div>

另外:您应该避免嵌套 Bootstrap 容器,除非绝对必要几乎从来没有这种情况

【讨论】:

  • 是的,使用图很好,但最后,列上的p-0 正在消除排水沟