【问题标题】:How to align card in a full height column vertically center in bootstrap-4? [duplicate]如何在 bootstrap-4 中垂直居中对齐全高列中的卡片? [复制]
【发布时间】:2020-07-04 06:37:49
【问题描述】:

所以我希望卡片中的这个表单位于整个网页的中间并具有响应性。我尝试将所有内容的高度更改为 h-100 和 align-items-center,但似乎没有任何效果。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <style>
      html,body{
        height: 100%;
      }
    </style>
    <title></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

  </head>
  <body>
    <div class="container h-100">
      <div class="d-flex justify-content-center h-100 ">
      <div class="col-md-6 h-100 ">
        <div class="card">
          <div class="card-body">
            <div class="card-title"><h3>Log In</h3></div>
              <form method="post">
                <div class="form-group">
                  <label for="username">Username</label>
                  <input class="form-control" type="text" name="username" value="Username" placeholder="Donald Trump...">
                </div>
                <div class="form-group">
                  <label for="username">Password</label>
                  <input class="form-control" type="password" name="password" value="Password" placeholder="**********">
                </div>
                  <input class="btn btn-primary" type="submit" name="" value="LogIn">
              </form>
                </div>
                </div>
          </div>
      </div>
      </div>


      <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

  </body>
</html>

我期待这个: enter image description here

但我明白了: enter image description here

【问题讨论】:

  • 尝试一些 CSS 到类卡片位置:absolute;top: 50%;transform: translateY(-50%);width: 100%;
  • 尝试在容器 div 上使用“d-flex justify-content-center align-items-center”,之后可能需要调整宽度
  • justify-content-center 和 align-items-center 适用于 flexbox 父级的子级(因为列应该在 row 内的行),所以列不应该是 @987654326 @codeply.com/p/RNbfSzVt4c

标签: html css forms bootstrap-4


【解决方案1】:

在 flexbox 容器上使用 align-items 实用程序来更改 flex 项目在交叉轴上的对齐方式(y 轴开始,x 轴如果 flex-direction: 列)。从开始、结束、中心、基线或拉伸中选择(浏览器默认)。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <style>
      html,body{
        height: 100%;
      }
    </style>
    <title></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

  </head>
  <body>
    <div class="container h-100">
      <div class="d-flex justify-content-center h-100 align-items-center">
      <div class="col-md-6">
        <div class="card">
          <div class="card-body">
            <div class="card-title"><h3>Log In</h3></div>
              <form method="post">
                <div class="form-group">
                  <label for="username">Username</label>
                  <input class="form-control" type="text" name="username" value="Username" placeholder="Donald Trump...">
                </div>
                <div class="form-group">
                  <label for="username">Password</label>
                  <input class="form-control" type="password" name="password" value="Password" placeholder="**********">
                </div>
                  <input class="btn btn-primary" type="submit" name="" value="LogIn">
              </form>
                </div>
                </div>
          </div>
      </div>
      </div>


      <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

  </body>
</html>

欲了解更多信息:https://getbootstrap.com/docs/4.0/utilities/flex/#align-items

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 2016-05-23
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2020-03-11
    • 2018-06-21
    • 2021-08-31
    相关资源
    最近更新 更多