引导卡-对齐卡页脚中的两个按钮

时间:2019-07-01 05:54:54

标签: css bootstrap-4

我正在使用Bootstrap Card,并且在页脚中有两个按钮。我需要做的是将一个按钮对准绝对中心,并将另一个较小的按钮对准页脚的左侧。并且两个按钮都应该在垂直方向上居中对齐。

这是我的代码:

<div class="card">
    <div class="card-header">Header</div>
    <div class="card-body">Content</div> 
    <div class="card-footer">      
      <div class="btn-wrapper text-center">
        <a class="btn btn-secondary text-dark btn-sm">Remove</a> 
        <a class="btn btn-warning" style="">Next</a>
      </div>
    </div>
  </div> 

这会将两张卡都对准中心。但是我需要“下一步”按钮位于中间,而“删除”按钮位于左侧。我该怎么办?

这是它的jsbin:https://jsbin.com/vesobayuci/1/edit?html,js,output

4 个答案:

答案 0 :(得分:1)

检查更新的JSBIN

.remove-btn {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
}

.btn-wrapper {
  position: relative;
}

答案 1 :(得分:0)

您可以像这样绝对定位它们

.card-footer .btn-wrapper{
  position: relative;
  min-height: 40px;
}

.card-footer .btn-wrapper .btn{
  position: absolute;
  top: 50%;
}

.card-footer .btn-wrapper .btn-warning{
  left: 50%;
  transform: translate(-50%, -50%);
}

.card-footer .btn-wrapper .btn-secondary{
  left: 0;
  transform: translate(0, -50%);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="container">
  <h2>Card Header and Footer</h2>
  <div class="card">
    <div class="card-header">Header</div>
    <div class="card-body">Content</div> 
    <div class="card-footer">      
      <div class="btn-wrapper text-center">
        <a class="btn btn-secondary pull-left text-dark btn-sm">Remove</a> 
        <a class="btn btn-warning" style="">Next</a>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

您可以使用transform属性和Bootstrap 4类完成此操作

.btn-warning {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Card Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>

<body>
  <div class="container">
    <h2>Card Header and Footer</h2>
    <div class="card">
      <div class="card-header">Header</div>
      <div class="card-body">Content</div>
      <div class="card-footer">
        <div class="btn-wrapper position-relative">
          <a class="btn btn-secondary text-dark btn-sm">Remove</a>
          <a class="btn btn-warning position-absolute">Next</a>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

答案 3 :(得分:0)

请检查演示 https://output.jsbin.com/cezegetufe

.btn-wrapper .btn-secondary {
  line-height: 1;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Card Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h2>Card Header and Footer</h2>
    <div class="card">
      <div class="card-header">Header</div>
      <div class="card-body">Content</div>
      <div class="card-footer">
        <div class="btn-wrapper text-center d-flex justify-content-between">
          <a class="btn btn-secondary  btn-sm text-white d-flex align-items-center">Remove</a>
          <a class="btn btn-warning" style="">Next</a>
        </div>
      </div>
    </div>
  </div>

</body>

</html>