How to Vertically Align Two <div> Elements with Bootstrap

Solutions for Bootstrap 3.4.1 and Bootstrap 4.5.0

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout.

In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property. We set the flex-direction property to "row".

Example of vertically aligning two <div> elements in Bootstrap 3.4.1:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <style>
      body {
        background: transparent;
      }
      .vertical-align {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: row;
      }
      .vertical-align > div:first-child {
        background-color: lightblue;
      }
      .vertical-align > div:last-child {
        background-color: lightgreen;
      }
      .big-box {
        height: 10em;
      }
      .small-box {
        height: 3em;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="row vertical-align">
        <div class="col-xs-6 col-md-8">
          <div class="big-box">Big</div>
        </div>
        <div class="col-xs-6 col-md-4">
          <div class="small-box">Small</div>
        </div>
      </div>
    </div>
  </body>
</html>

In the following example, we set the display of "vcenter" to "inline-block", the vertical-align property to "middle" and specify the float property.

Example of vertically aligning two <div> elements:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <style>
      body {
        background: transparent;
      }
      .vcenter {
        display: inline-block;
        vertical-align: middle;
        float: none;
        max-width: 49%;
      }
      .big-box {
        height: 8em;
        border: 1px solid purple;
        background: red;
      }
      .small-box {
        height: 2em;
        border: 1px solid green;
        background: green;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-xs-6 vcenter">
          <div class="big-box">Big</div>
        </div>
        <div class="col-xs-6 vcenter">
          <div class="small-box">Small</div>
        </div>
      </div>
    </div>
  </body>
</html>

Example of vertically aligning two <div> elements in Bootstrap 4.5.0:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <style>
      html,
      body {
        height: 100%;
        background: transparent;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-6 align-self-center">
          <div class="card card-body">Text</div>
        </div>
        <div class="col-6">
          <div class="card text-white bg-danger">
            <div class="card-body">
              <h3 class="card-title">Taller box</h3>
              <p class="card-text">
                HTML, an acronym for HyperText Markup Language, is a primary markup language for creating websites. It consists of a series of codes used to structure texts, images, and other content to be displayed in a browser.
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

Do you find this helpful?

Related articles