引导卡 - 在移动设备上查看时出现居中问题

Posted

技术标签:

【中文标题】引导卡 - 在移动设备上查看时出现居中问题【英文标题】:Bootstrap Cards - issue with centering when viewed on mobile device 【发布时间】:2019-06-13 04:30:46 【问题描述】:

我对此进行了广泛的研究,但无济于事。我正在使用以下代码输出其下方的屏幕截图,但在移动设备上查看时,它会向左移动。

谁能帮我解决这个问题?提前致谢!

<div class="container">
    <div class="row g-mb-20">
      <div class="col-md-6 col-lg-3 g-mb-50 justify-content-center">
        <div class="card" style="width: 18rem;">
        <img src="assets/img/calisurf1.jpg" class="card-img-top" >
          <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 class="col-md-6 col-lg-3 g-mb-50 justify-content-center">
        <div class="card" style="width: 18rem;">
        <img src="assets/img/calisurf1.jpg" class="card-img-top" >
          <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 class="col-md-6 col-lg-3 g-mb-50 justify-content-center">
        <div class="card" style="width: 18rem;">
        <img src="assets/img/calisurf1.jpg" class="card-img-top" >
          <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 class="col-md-6 col-lg-3 g-mb-50 justify-content-center">
        <div class="card" style="width: 18rem;">
        <img src="assets/img/calisurf1.jpg" class="card-img-top" >
          <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>
  </div>

Desktop Version

Mobile Version

【问题讨论】:

您应该添加 col-12,以便它在移动设备上占据整个屏幕 添加 col-12 或 col-sm-12 不会使卡片全宽。真的很感激这个想法,并希望这能发生。 是的,我的错。我认为您的 style="width: 18rem" 导致了这个问题。 就是这样,谢谢!每个边缘都有一点边距,但我认为负边距可以解决这个问题,或者可能是“容器流体”。默认情况下可能是 Bootstrap 的排水沟。 【参考方案1】:

您只需将d-flex 类添加到columns 容器(您已经拥有justify-content-center 类):

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>


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

  <div class="col-md-6 col-lg-3 mb-5 d-flex justify-content-center">
    <div class="card w-100">
      <img src="https://via.placeholder.com/50" class="card-img-top" >
      <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 class="col-md-6 col-lg-3 mb-5 d-flex justify-content-center">
    <div class="card w-100">
      <img src="https://via.placeholder.com/50" class="card-img-top" >
      <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 class="col-md-6 col-lg-3 mb-5 d-flex justify-content-center">
    <div class="card w-100">
      <img src="https://via.placeholder.com/50" class="card-img-top" >
      <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 class="col-md-6 col-lg-3 mb-5 d-flex justify-content-center">
    <div class="card w-100">
      <img src="https://via.placeholder.com/50" class="card-img-top" >
      <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>
</div>

【讨论】:

d-flex 成功了!太感谢了!一旦我达到 15 个代表标记,我一定会回到这里并为你投票。现在要弄清楚如何在移动设备上获得全宽卡片... 也陷入了困境。谢谢你。 +1

以上是关于引导卡 - 在移动设备上查看时出现居中问题的主要内容,如果未能解决你的问题,请参考以下文章

使用媒体查询在移动设备上隐藏引导工具提示

导航栏引导菜单未在移动设备中显示

汉堡菜单未在移动设备上显示

在移动设备上垂直堆叠的引导列

引导数据切换在移动设备上不起作用

可折叠的 div,但仅在移动设备上