How to use vertical align in bootstrap
Solution 1:
.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.
Solution 2:
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!
Solution 3:
For the parent: display: table;
For the child: display: table-cell;
Then add vertical-align: middle;
I can make a fiddle but home time now, yay!
OK here is the lovely fiddle: http://jsfiddle.net/lharby/AJAhR/
.parent {
display:table;
}
.child {
display:table-cell;
vertical-align:middle;
text-align:center;
}
Solution 4:
I had to add width: 100%; to display table to fix some strange bahavior in IE and FF, when i used this example. IE and FF had some problems displaying the col-md-* tags at the right width
.display-table {
display: table;
table-layout: fixed;
width: 100%;
}
.display-cell {
display: table-cell;
vertical-align: middle;
float: none;
}
Solution 5:
http://jsfiddle.net/b9chris/zN39r/
HTML:
<div class="item row">
<div class="col-xs-12 col-sm-6"><h4>This is some text.</h4></div>
<div class="col-xs-12 col-sm-6"><h4>This is some more.</h4></div>
</div>
CSS:
div.item div h4 {
height: 60px;
vertical-align: middle;
display: table-cell;
}
Important notes:
- The
vertical-align: middle; display: table-cell;
must be applied to a tag that has no Bootstrap classes applied; it cannot be acol-*
, arow
, etc. - This can't be done without this extra, possibly pointless tag in your HTML, unfortunately.
- The backgrounds are unnecessary - they're just there for demo purposes. So, you don't need to apply any special rules to the
row
orcol-*
tags. - It is important to notice the inner tag does not stretch to 100% of the width of its parent; in our scenario this didn't matter but it may to you. If it does, you end up with something closer to some of the other answers here:
http://jsfiddle.net/b9chris/zN39r/1/
CSS:
div.item div {
background: #fdd;
table-layout: fixed;
display: table;
}
div.item div h4 {
height: 60px;
vertical-align: middle;
display: table-cell;
background: #eee;
}
Notice the added table-layout and display properties on the col-*
tags. This must be applied to the tag(s) that have col-*
applied; it won't help on other tags.