I'm a new developer trying to get a bootstrap site up and running. Of course, I have encountered typical newbie problems with vertical alignment. I thought that my problems would be solved (at least for the case of vertical centering) by adding a class .vcenter
to my .row
elements. The .vcenter
class (basically) has the display: flex
and align-items: center
property/value pairs. This idea came from an excellent SO answer on vertical alignment by Hashem Qolami.
HOWEVER, when I resize the browser window, the grid columns do not stack like they should, even when I apply the .col-xs-12
class to each column. Instead, if the window gets small enough, the columns overlap in a very ugly way. Here is a bootply with the relevant html/css. To see what I'm talking about, navigate to the bootply linked above and click on the phone icon to open a mobile sized screen. If you shrink the window down small enough there's an ugly overlap of the columns. More importantly, the columns should be stacking (as you can see if you turn off the .vcenter
class).
SO THE QUESTION IS can I use flexboxes and the bootstrap grid system at the same time or is there some inherent lack of compatibility between the two for this particular case? I suppose possible solutions would be to turn off the display: flex
property for mobile screens or to use inline-block
for vertical alignment, but I'm looking for a more fundamental theoretical explanation of what's going on here.
Here's the code from the bootply:
HTML
<!DOCTYPE html>
<html>
<head><!-- bootstrap css automatically included by bootply --></head>
<body>
<div class="container">
<div class="row vcenter">
<div class="col-xs-12 col-md-5 bluebox">
<h1>Big Blue Box</h1>
</div>
<div class="col-xs-12 col-md-7 redbox">
<h1>Big Red Box</h1>
</div>
</div>
</div>
</body>
</html>
CSS
/* CSS used here will be applied after bootstrap.css */
.bluebox {
color: #FFF;
background-color: blue;
}
.redbox {
color: #FFF;
background-color: red;
}
/* FLEXBOX CLASSES */
.vcenter {
min-height: 100%; /* Fallback for vh unit */
min-height: 100vh;
display: flex;
align-items: center;
/* Bootply includes properties for cross-browser compatibility */
}