Simple problem: How do I vertically align a col within a col using bootstrap? Example here (I want to vertically align child1a and child1b):
http://bootply.com/73666
HTML
<div class="col-lg-12"> <div class="col-lg-6 col-md-6 col-12 child1"> <div class="col-12 child1a">Child content 1a</div> <div class="col-12 child1b">Child content 1b</div> </div> <div class="col-lg-6 col-md-6 col-12 child2"> Child content 2<br> Child content 2<br> Child content 2<br> Child content 2<br> Child content 2<br> </div> </div>
UPDATE
Some CSS:
.col-lg-12{ top:40px; bottom:0px; border:4px solid green; } .child1a, .child1b{ border:4px solid black; display:inline-block !important; } .child1{ border:4px solid blue; height:300px; display:table-cell !important; vertical-align:middle; } .child2{ border:4px solid red; }
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.
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.
Aligning content to the center of the DIV is very simple in Bootstrap 5, You just have to convert the div into a flex box using d-flex class property and then use the property align-items-center to vertically align the content in the middle of the div.
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.
.parent { display: table; table-layout: fixed; } .child { display:table-cell; vertical-align:middle; text-align:center; }
table-layout: fixed
prevents breaking the functionality of the col-* classes.
Maybe an old topic but if someone needs further help with this do the following for example (this puts the text in middle line of image if it has larger height then the text).
HTML:
<div class="row display-table"> <div class="col-xs-12 col-sm-4 display-cell"> img </div> <div class="col-xs-12 col-sm-8 display-cell"> text </div> </div>
CSS:
.display-table{ display: table; table-layout: fixed; } .display-cell{ display: table-cell; vertical-align: middle; float: none; }
The important thing that I missed out on was "float: none;" since it got float left from bootstrap col attributes.
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With