How to set vertical alignment in Bootstrap ?

30 Nov, 2021
Vertical Alignment changes the alignment of elements vertically with the help of vertical-alignment utilities. The vertical-align utilities only affect inline(Present in one Line), inline-block(Present as blocks in one line), inline-table, and table cell(Elements in a cell of a table) elements. Vertical Alignment of div is one of the most basic requirements of a responsive web page. This can be achieved through CSS but the Bootstrap library has some classes specifically built for this purpose. In this article, we will learn the available classes & methods used for vertical-align in Bootstrap. Please refer to Vertical alignment in Bootstrap with Examples for other vertical-align classes.
Here, we will discuss 2 built-in classes:
Using class align-items-center
Using class d-flex with class align-items-center
Let’s understand both the classes through examples.
Method 1: Using class align-items-center
In Bootstrap 5, if we want to vertically align an <div> element in the center, we can do this by applying the class align-items-center on the containing element of that div.
Example:
HTML
<!DOCTYPE html><html lang="en">  <head>    <meta charset="utf-8"/>    <meta name="viewport" content="width=device-width, initial-scale=1" />     <link      rel="stylesheet"      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">    </script>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">    </script>    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">    </script>  </head>   <body>    <div class="container">      <div        class="row align-items-center bg-success text-light"        style="min-height: 100vh">        <div class="col-md-12">          <h1>GeeksforGeeks</h1>        </div>      </div>    </div>  </body></html>
Output:
Method 2: Using class d-flex with class align-items-center
In Bootstrap 5, if we want to vertically align an <div> element in the middle of a containing element, we can do this by applying the class align-items-center and d-flex on the containing element of that div. Here, the d-flex class has the same effect as that of the display: flex; property in CSS.
Example:
HTML
<!DOCTYPE html><html lang="en">  <head>    <meta charset="utf-8"/>    <meta name="viewport" content="width=device-width, initial-scale=1"/>     <link      rel="stylesheet"      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">    </script>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">    </script>    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">    </script>  </head>   <body>    <div class="d-flex align-items-center" style="min-height: 100vh">      <div class="box w-100 text-success">        <h1>GeeksforGeeks</h1>      </div>    </div>  </body></html>
Output:
nnr223442
Bootstrap-Questions
Picked
Bootstrap
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Show Images on Click using HTML ?
How to Use Bootstrap with React?
Tailwind CSS vs Bootstrap
How to toggle password visibility in forms using Bootstrap-icons ?
How to place table text into center using Bootstrap?
How to update Node.js and NPM to next version ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
REST API (Introduction)
Hide or show elements in HTML using display property