Home > Software engineering >  Center div vertically in Bootstrap 5
Center div vertically in Bootstrap 5

Time:03-20

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 >
        <div>div that should be centered</div>
    </div>

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

CodePudding user response:

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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  </head>
  <body>
    <div >
      <div>This div is not centered.</div>
      <div >
        <div>This div is centered.</div>
      </div>
    </div>
  </body>
</html>

  • Related