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.
Update 2018
I know the original question was for Bootstrap 3, but now that Bootstrap 4 has been released, here is some updated guidance on vertical center.
Important! Vertical center is relative to the height of the parent
Bootstrap 4
Now that BS4 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. Here are some Bootstrap 4 vertical centering options..
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, andmy-auto
will vertically center thecol-sm-12
column.BS4 - Vertical center using auto-margins Demo
my-auto
represents margins on the vertical y-axis and is equivalent to:2 - Vertical Center with Flexbox:
Since Bootstrap 4
.row
is nowdisplay:flex
you can simply usealign-self-center
on any column to vertically center it...or, use
align-items-center
on the entire.row
to vertically center align allcol-*
in the row...BS4 - Vertical center different height columns Demo
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.BS4 - Vertical center using display utils Demo
Also see: Vertical Align Center in Bootstrap 4
Bootstrap 3
Flexbox method on the container of the item(s) to center:
Transform translateY method:
Display inline method:
Demo of Bootstrap 3 centering methods
You still can use a custom class when you need it:
Bootply
Using
inline-block
adds extra space between blocks if you let a real space in your code (like...</div> </div>...
). This extra space breaks our grid if column sizes add up to 12:Here, we've got extra spaces between
<div class="[...] col-lg-2">
and<div class="[...] col-lg-10">
(a carriage return and 2 tabs/8 spaces). And so...Let's kick this extra space!!
Notice the seemingly useless comments
<!-- ... -->
? They are important -- without them, the whitespace between the<div>
elements will take up space in the layout, breaking the grid system.Note: the Bootply has been updated
I ran into the same situation where I wanted to align a few div elements vertically in a row and found that Bootstrap classes col-xx-xx applies style to the div as float: left.
I had to apply the style on the div elements like style="Float:none" and all my div elements started vertically aligned. Here is the working example:
JsFiddle Link
Just in case someone wants to read more about the float property:
W3Schools - Float
I just did this and it does what I want it to do.
And now my page looks like
I genuinely find the following code works using Chrome and not other browsers than the currently selected answer:
Bootply Link
You may need to amend the heights (specifically on
.v-center
) and remove/change div ondiv[class*='col-']
for your needs.