Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical alignment in Bootstrap 4

I have following setup in Bootstrap 4: Bootply link

The element with the "Supplier" text in it should be vertically centered, I have different rows with the same code and I want them centered. No solution seems to work for me.

Can someone here find what I did wrong and point me in the right direction?

Thank you.

like image 997
Nelsch Avatar asked Dec 21 '16 14:12

Nelsch


People also ask

How do I vertically align text in Bootstrap 4?

One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

How do I vertically align a div in Bootstrap 4?

Answer: Use the align-items-center Class In Bootstrap 4, if you want to vertically align a <div> element in the center or middle, you can simply apply the class align-items-center on the containing element.

How do I center text vertically in Bootstrap?

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.


2 Answers

You can use the flex-xs-middle class like this..

Bootstrap 4 Alpha 5

<div class="container-fluid">     <div class="row">         <div class="col-xs-6">             <div class="circle-medium backgrounds"></div>         </div>         <div class="col-xs-6 flex-xs-middle">             <div class="name">Supplier</div>         </div>     </div>     <div class="row">         <div class="col-xs-6">             <div class="circle-medium backgrounds"></div>         </div>          <div class="col-xs-6 flex-xs-middle">             <div class="name">Supplier</div>         </div>     </div> </div> 

http://www.codeply.com/go/PNNaNCB4T5 (Using the Bootstrap 4 flexbox enabled CSS)

Bootstrap 4


UPDATE for Bootstrap 4.0.0

Now that Bootstrap 4 is flexbox by default there are many different approaches to vertical alignment using: auto-margins, flexbox utils, or the display utils along with vertical align utils. At first "vertical align utils" seems obvious but these only work with inline and table display elements. Below are the Bootstrap 4 vertical centering options. Remember, vertical alignment is relative to parent height.


1 - Vertical Center Using Auto Margins:

Another way to vertically center is to use my-auto. This will center the element within it's container. For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

<div class="row h-100">     <div class="col-sm-12 my-auto">         <div class="card card-block w-25">Card</div>     </div> </div> 

Vertical Center Using Auto Margins Demo

my-auto represents margins on the vertical y-axis and is equivalent to:

margin-top: auto; margin-bottom: auto; 

2 - Vertical Center with Flexbox:

vertical center grid columns

Since Bootstrap 4 .row is now display:flex you can simply use align-self-center on any column to vertically center it...

       <div class="row">            <div class="col-6 align-self-center">                 <div class="card card-block">                  Center                 </div>            </div>            <div class="col-6">                 <div class="card card-inverse card-danger">                     Taller                 </div>           </div>     </div> 

or, use align-items-center on the entire .row to vertically center align all col-* in the row...

       <div class="row align-items-center">            <div class="col-6">                 <div class="card card-block">                  Center                 </div>            </div>            <div class="col-6">                 <div class="card card-inverse card-danger">                     Taller                 </div>           </div>     </div> 

Vertical Center Different Height Columns Demo

Important: The direct parent of the element to be aligned must have a defined height!


3 - Vertical Center Using Display Utils:

Bootstrap 4 has display utils that can be used for display:table, display:table-cell, display:inline, etc.. These can be used with the vertical alignment utils to align inline, inline-block or table cell elements.

<div class="row h-50">     <div class="col-sm-12 h-100 d-table">         <div class="card card-block d-table-cell align-middle">             I am centered vertically         </div>     </div> </div> 

Vertical Center Using Display Utils Demo

like image 67
Zim Avatar answered Sep 17 '22 21:09

Zim


To have a main element on the page vertically centered using .my-auto class, applying full-height on every element before it is required; using one of the following:

  • .h-100 Bootstrap class
  • height: 100vh; style
  • height: 100%; style

Example:

<body class="h-100">   <div class="container h-100">     <div class="row h-100">       <div class="col-12 my-auto"> <!-- Finally! -->         <div class="jumbotron">           ...         </div>       </div>     </div>   </div> </body> 
like image 21
Sky7ure Avatar answered Sep 16 '22 21:09

Sky7ure