Center div vertically in Bootstrap 5

Viewed 1400

How can I center a div vertically in bootstrap 5? It should be aligned in the middle between of content before the div and the screen end.

Tried this:

    <div>some content</div>
    <div class="d-flex flex-column min-vh-100 justify-content-center align-items-center">
        <div>div that should be centered</div>
    </div>

It makes the pages longer than it actually is. The div doesn´t looks centered.

2 Answers

You can do it like this:

  • Wrap everything with one div that has 100% screen width and height.
  • Use flex-grow-1 for the second div so it takes the remaining height and center everything inside.

<!doctype html>
<html lang="en">
  <head>
    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  </head>
  <body>
    <div class="d-flex flex-column min-vh-100 min-vw-100">
      <div>This div is not centered.</div>
      <div class="d-flex flex-grow-1 justify-content-center align-items-center">
        <div>This div is centered.</div>
      </div>
    </div>
  </body>
</html>

<!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" />
    <title>Document</title>
    <!-- CSS only -->
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor"
      crossorigin="anonymous"
    />
    <style>
      .card
      {
        min-width: 320px;
        width: 480px;
      }
    </style>
  </head>
  <body>
    <div class="d-flex align-items-center justify-content-center vh-100">
    <div class="card shadow-lg">
      <div class="card-header text-bg-danger">
        <h2>
          <i class="fa-solid fa-user"></i>
          Login
        </h2>
      </div>
      <div class="card-body">
        <form action="">
          <div class="mb-3">
            <label for="email" class="form-label">Email address</label>
            <input
              placeholder="your registered email"
              class="form-control"
              type="email"
              name="email"
              id="email"
            />
          </div>
          <div class="mb-3">
            <label for="password" class="form-label">Password</label>
            <input
              placeholder="your password"
              class="form-control"
              type="password"
              name="password"
              id="password"
            />
          </div>
          <div class="d-flex justify-content-end">
            <button class="btn btn-danger">Login</button> &nbsp;
            <button type="reset" class="btn btn-secondary">Reset all</button>
          </div>
        </form>
      </div>
    </div>
</div>
    <script
      src="https://kit.fontawesome.com/266b76cb57.js"
      crossorigin="anonymous"
    ></script>
    <!-- JavaScript Bundle with Popper -->
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2"
      crossorigin="anonymous"
    ></script>
  </body>
</html>
Related