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.
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 =
< script src =
</ script >
< script src =
</ script >
< script src =
</ 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 =
< script src =
</ script >
< script src =
</ script >
< script src =
</ 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:
Last Updated :
30 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...