I'm using Twitter Bootstrap 3, and I have problems when I want to align vertically two div
, for example — JSFiddle link:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-xs-5">
<div style="height:5em;border:1px solid #000">Big</div>
</div>
<div class="col-xs-5">
<div style="height:3em;border:1px solid #F00">Small</div>
</div>
</div>
The grid system in Bootstrap uses float: left
, not display:inline-block
, so the property vertical-align
doesn't work. I tried using margin-top
to fix it, but I think this is not a good solution for the responsive design.
This is my solution. Just add this class to your css.
Then your HTML would look like this.
If you are using the Less version of Bootstrap, and compiling into CSS yourself, you can use some utility classes to use with Bootstrap classes.
I've found these to work fantastically well where I want to preserve the responsiveness and configurability of the Bootstrap grid system (like using
-md
or-sm
), but I want all columns in a given row to all have the same vertical height (so that I can then vertically align their content and have all columns in the row share a common middle).CSS/Less:
HTML:
I thought I'd share my "solution" in case it helps anyone else who isn't familiar with the @media queries themselves.
Thanks to @HashemQolami's answer, I built some media queries that would work mobile-up like the col-* classes so that I could stack the col-* for mobile but display them vertically-aligned in the center for larger screens, e.g.
.
More complicated layouts that require a different number of columns per screen resolution (e.g. 2 rows for -xs, 3 for -sm, and 4 for -md, etc.) would need some more advanced finagling, but for a simple page with -xs stacked and -sm and larger in rows, this works fine.
The below code worked for me:
I prefer this method as per David Walsh Vertical center CSS:
The
transform
isn't essential; it just finds the center a little more accurately. Internet Explorer 8 may be slightly less centered as a result, but it is still not bad - Can I use - Transforms 2d.I ran into this same issue. In my case I did not know the height of the outer container, but this is how I fixed it:
First set the height for your
html
andbody
elements so that they are 100%. This is important! Without this, thehtml
andbody
elements will simply inherit the height of their children.Then I had an outer container class with:
And lastly the inner container with class:
HTML is as simple as:
This is all you need to vertically align contents. Check it out in fiddle:
Jsfiddle