How to set vertical alignment in Bootstrap ?
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.
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.