Bootstrap - 对齐按钮到卡的底部

时间:2018-01-23 16:32:29

标签: html css twitter-bootstrap bootstrap-4

我正在查看一个使用card-deckcard类(Pricing example)的Bootstrap示例。我想知道如果其中一个列表的项目少于其他列表,那么如何修复按钮对齐。

Alignment issue

我希望所有按钮都垂直对齐(在每张卡的底部),但我无法找到一种方法。我尝试设置.align-bottom类以及在<div class="align-text-bottom">中包装按钮。我还尝试了this question about adding space的一些建议,但仍然没有成功(间距应该是变量的,以便它填满列表中的剩余空间)。

包装在<div class="card-footer bg-white">中并没有产生预期的效果,因为它没有对齐卡片底部的按钮,而是在它周围创建了某种边框。

有没有人有想法?

修改:这是一个类似问题的jsfiddle

7 个答案:

答案 0 :(得分:60)

根据添加jsfiddle链接

进行了更新

您可以使用以下Bootstrap 4修饰符类:

  1. d-flex添加到.card-body
  2. flex-column添加到.card-body
  3. mt-auto添加到嵌套在.btn
  4. 中的.card-body

    updated fiddle

    有关Bootstrap 4的Flexbox修改类的完整列表,请参阅this page

答案 1 :(得分:6)

类似的问题是answered here

只需将align-self-end类添加到项目底部即可对齐。

https://www.codeply.com/go/Fiorqv1Iz6

     <div class="card-body d-flex flex-column">
            <h1 class="card-title pricing-card-title">$15 <small class="text-muted">/ mo</small></h1>
            <ul class="list-unstyled mt-3 mb-4">
              <li>20 users included</li>
              <li>10 GB of storage</li>
            </ul>
            <button type="button" class="align-self-end btn btn-lg btn-block btn-primary" style="margin-top: auto;">Get started</button>
     </div>

默认情况下,card显示为:flex,但card-body不显示。因此,您需要将d-flex flex-column添加到card-body。这将使flexbox对齐类起作用。

另一种选择是在按钮上使用mt-auto(自动上边距),将其推到卡的底部。

答案 2 :(得分:2)

VALUES('O19001', DATE '1996-01-12','C00001', 'F', 'N', 'S00001', DATE '1996-01-20', 'IN PROCESS'); div设置为.card-bodydisplay:flex

然后按下按钮flex-direction:column

我想有一些Bootstrap帮助类。

&#13;
&#13;
margin-top:auto
&#13;
.card-body {
  display: flex;
  flex-direction: column;
}

button.btn {
  margin-top: auto;
}
&#13;
&#13;
&#13;

答案 3 :(得分:2)

使用页脚,它已经为您完成了所有设置。

<div class="card-deck">
<div class="card">
   <div class="card-body">
      <h4 class="card-title">Title goes here</h4>
      <p class="card-text">Ut leo enim, fermentum fermentum tempor sit amet, vehicula in felis. Pellentesque a arcu augue. Nam eu malesuada lorem. Curabitur et molestie lacus.</p>
   </div>
   <div class="card-footer text-muted mx-auto">
      <button type="button" class="btn btn-sm btn-outline-secondary">Click me!</button>
   </div>
</div>

然后,您可以选择设置card-footer元素的样式。

.card-footer {
  background: transparent;
  border-top: 0px;
}

演示:https://jsfiddle.net/rustynox/203zwyq6/

答案 4 :(得分:0)

Flex是你的朋友

像这样的东西将起到魔力:

&#13;
&#13;
.flex-wrap {
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: inherit;
  -ms-flex-align: inherit;
  align-items: inherit;
}

.flex-container {
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  background: #eee;
  border: 1px solid #ccc;
  margin: 10px;
  padding: 10px;
}


.flex-item {
  -webkit-flex: 0 1 auto;
  -ms-flex: 0 1 auto;
  flex: 0 1 auto;
}


.fill{
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.btn{
  background:#069;
  padding:10px;
  color:#fff;
}
&#13;
<div class="flex-wrap">

  <div class="flex-container">
    <div class="flex-item">FREE</div>
    <div class="flex-item fill">
      <h2>$0</h2>
      <p>Some text ... ashd iaush diuhasd uhasd aiusdh iaush d haisduhaiusdh iaush d haisduh aisuhd ias u</p>
    </div>
    <div class="flex-item">
      <a href="#" class="btn">SIGN UP</a>
    </div>
  </div>

  <div class="flex-container">
    <div class="flex-item">PRO</div>
    <div class="flex-item fill">
      <h2>$10</h2>
      <p>Some text ... ashd iaush uhasd aiusdh iaush d haisduhdiuhasd aiusdh iuhasd aiusdh iaush d haisduhaush d haisduh aisuhd ias u</p>
    </div>
    <div class="flex-item">
      <a href="#" class="btn">GET STARTED</a>
    </div>
  </div>

  <div class="flex-container">
    <div class="flex-item">ENTERPRISE</div>
    <div class="flex-item fill">
      <h2>$20</h2>
      <p>Some text ... ashd iaush diuhasd aiusdh iaush d haisduh aisuhd ias u</p>
    </div>
    <div class="flex-item">
      <a href="#" class="btn">CONTACT</a>
    </div>
  </div>
  
</div>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

您必须添加:

<div class="card-footer">
<button type="button" class="btn btn-primary btn-sm btn-block" onclick="location.href = '';">BUY NOW </button>
</div>

答案 6 :(得分:0)

用包含 mt-auto

类的 div 标签包围卡片中的按钮

<div class="d-flex flex-row justify-content-center mt-auto">
<a href="#" target="_blank" target="_blank" type="button" class="btn btn-primary mr-3">GitHub</a>     
</div> 

相关问题