Bootstrap 5表单的水平和垂直居中

4
我正在尝试将我的Bootstrap表单水平和垂直对齐,使其位于屏幕中央;然而,我找不到任何解决方案来帮助我。
我尝试使用这些Bootstrap类,但它们没有起作用: d-flex align-items-center justify-content-center 这是我的表单:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
  <title>Login</title>
</head>

<body>
  <div class="d-flex align-items-center justify-content-center">
    <form>
      <div class="mb-3 form-control-sm">
        <label for="loginInputEmail" class="form-label">Email address</label>
        <input type="email" class="form-control" id="loginInputEmail" aria-describedby="emailHelp">
        <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
      </div>
      <div class="mb-3 form-control-sm">
        <label for="loginInputPassword" class="form-label">Password</label>
        <input type="password" class="form-control" id="loginInputPassword">
      </div>
      <div class="mb-3 form-control-sm">
        <label for="selectAccountType" class="form-label">Account type</label>
        <select class="form-select form-select-sm" id="selectAccountType" aria-label="selectAccountType">
          <option selected>Account type</option>
          <option value="1">Arbetare</option>
          <option value="2">Företag</option>
          <option value="3">Admin</option>
        </select>
      </div>
      <div class="mb-3 form-check form-control-sm">
        <input type="checkbox" class="form-check-input" id="rememberCheck">
        <label class="form-check-label" for="rememberCheck">Remember me</label>
      </div>
      <button type="submit" class="btn btn-primary">Login</button>
    </form>
  </div>
</body>

</html>


你能上传你的代码并尝试使用填充吗? - Aahad
现在我已经添加了代码,最好使用Bootstrap类来完成它。 - user14353062
1
为什么要使用填充(Padding)@Aahad? - Sfili_81
代码嵌块中您遇到了什么问题? - Sfili_81
它对我不起作用,它只是水平居中对齐,但垂直方向上并没有居中,它不在屏幕中央。 - user14353062
2个回答

14

看起来父级 div 需要一个垂直高度,可以使用 vh-100 让其使用视口高度的 100%:

  <body>
    <!-- vh-100 here-->
    <div class="d-flex align-items-center justify-content-center vh-100">
      <form>
        <div class="mb-3 form-control-sm">
          <label for="loginInputEmail" class="form-label">Email address</label>
          <input type="email" class="form-control" id="loginInputEmail" aria-describedby="emailHelp">
          <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
        </div>
        <div class="mb-3 form-control-sm">
          <label for="loginInputPassword" class="form-label">Password</label>
          <input type="password" class="form-control" id="loginInputPassword">
        </div>
        <div class="mb-3 form-control-sm">
          <label for="selectAccountType" class="form-label">Account type</label>
          <select class="form-select form-select-sm" id="selectAccountType" aria-label="selectAccountType">
            <option selected>Account type</option>
            <option value="1">Arbetare</option>
            <option value="2">Företag</option>
            <option value="3">Admin</option>
          </select>
        </div>
        <div class="mb-3 form-check form-control-sm">
          <input type="checkbox" class="form-check-input" id="rememberCheck">
          <label class="form-check-label" for="rememberCheck">Remember me</label>
        </div>
        <button type="submit" class="btn btn-primary">Login</button>
      </form>
    </div>
  </body>

1
你解决了我两天的工作,非常感谢你提供的解决方案和解释。 - Sajeeb Chandan Saha

-4

它正好做了应该做的事情。 你的父类:<div class="d-flex align-items-center justify-content-center"> 没有设置高度。 这意味着你的表单将占用所有可用空间。

如果你想要水平和垂直居中,你可以将父类的高度设置为:100vh

我更新了你的代码:https://codepen.io/Qontrol/pen/xxRqgJG


4
链接失效,踩个负面评价。这就是为什么使用外部代码不好。 - b264

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接