繁体   English   中英

使用 bootstrap 4.3.1 时如何将卡片列放在页面中心?

[英]How to place card-columns in the center of the page when using bootstrap 4.3.1?

我正在尝试在嵌套卡片时使用Bootstrap 4.3.1卡片列水平居中。

这是我的卡片的嵌套方式

 > Main Card
   >> Card-columns
      >>> Card 1  
      >>> Card 2     
      >>> Card 3 
      >>> Card N

我试图将mx-auto class 放在主卡和卡列上,但这并没有将卡列放在页面的中心。

这是我的代码

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <main role="main"> <div class="container-fluid"> <div class="mx-auto card bg-light"> <div class="card-body"> <h2 class="card-title text-center text-uppercase text-info">Cards</h2> <hr /> <div class="card-columns mx-auto"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 1</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 2</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> </div> </div> </div> </main>

我创建了一个提琴手来演示我到目前为止所做的事情。

如何正确地将卡片列放置在屏幕中央?

您需要将容器div设置为 flex-box,然后您可以应用所需的引导类。

在这种情况下,您需要:
<div class="card-columns mx-auto d-flex justify-content-center col-12">

因此,因为您想将card-columns居中,很明显justify-content-center可以完成这项工作。 对于您的card-columns宽度,您可以使用bootstrap中的 class col 基本上,它提供了最多 12 列的网格,这将占据整个页面的视图。 现在,您可以通过将col-[0 to 12]添加到最多 12 来告诉每张卡需要多少 col-span,因为您已经设置了它们的父标签col-12

如果它有col-8 ,那么您的卡片最多只能占用 8 列。

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <main role="main"> <div class="container-fluid"> <div class="mx-auto card bg-light"> <div class="card-body"> <h2 class="card-title text-center text-uppercase text-info">Cards</h2> <hr /> <div class="card-columns mx-auto d-flex justify-content-center col-12"> <div class="card text-center col-4"> <div class="card-body"> <h5 class="card-title">Card 1</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="card text-center col-4"> <div class="card-body"> <h5 class="card-title">Card 2</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> </div> </div> </div> </main>

card-colums删除嵌套卡片的 flexbox。 它变成inline-block ,你必须将你的卡与其他技巧对齐。

https://getbootstrap.com/docs/4.3/components/card/#card-columns

您可以做的是使用card-deck ,此时您可以放置mx-5而不是 auto。 这样每张卡片将占用剩余宽度的 50%。 在这种情况下,mx-auto 将不起作用,因为卡片将占据整个宽度,将其分成两部分, mx-auto将查找剩余宽度并在每一侧应用边距。 在这种情况下,每张卡片将占用 50%,因此没有空间留给mx-auto

mx-5或从 0 到 5 的任何值在嵌套元素获得它们各自的宽度之前应用。

您还可以使用card-deck align-items-center并定义每辆车的宽度。

最后,问题是您尝试将 inline-block 和 flex 一起使用,但您需要做出选择。

我无法使用card-columns完成它,但我使用card with fixed with完成了它

这是一个带有更新代码的jsFiddle https://jsfiddle.net/dLbnvatm/

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <main role="main"> <div class="container-fluid"> <div class="mx-auto card bg-light"> <div class="card-body"> <h2 class="card-title text-center text-uppercase text-info">Cards</h2> <hr /> <div class="mx-auto d-flex justify-content-center row"> <div class="col-sm-6 col-md-4 col-xl-3 p-1 m-0"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 1</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> <div class="col-sm-6 col-md-4 col-xl-3 p-1 m-0"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 2</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> <div class="col-sm-6 col-md-4 col-xl-3 p-1 m-0"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 3</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> <div class="col-sm-6 col-md-4 col-xl-3 p-1 m-0"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 4</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> <div class="col-sm-6 col-md-4 col-xl-3 p-1 m-0"> <div class="card text-center"> <div class="card-body"> <h5 class="card-title">Card 5</h5> <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> </div> </div> </div> </div> </main>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM