I'm using Bootstrap. How can I make three columns all the same height?
Here is a screenshot of the problem. I would like the blue and red columns to be the same height as the yellow column.
Here is the code:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-xs-4 panel" style="background-color: red">
some content
</div>
<div class="col-xs-4 panel" style="background-color: yellow">
catz
<img width="100" height="100" src="https://lorempixel.com/100/100/cats/">
</div>
<div class="col-xs-4 panel" style="background-color: blue">
some more content
</div>
</div>
</div>
Some of the previous answers explain how to make the divs the same height, but the problem is that when the width is too narrow the divs won't stack, therefore you can implement their answers with one extra part. For each one you can use the CSS name given here in addition to the row class that you use, so the div should look like this if you always want the divs to be next to each other:
For all screens:
For when you want to use sm:
For when you want to md:
For when you want to use lg:
EDIT Based on a comment, there is indeed a simpler solution, but you need to make sure to give column info from the largest desired width for all sizes down to xs (e.g.
<div class="col-md-3 col-sm-4 col-xs-12">
:If it makes sense in your context, you can simply add an empty 12-column div after each break, which acts as a divider that hugs the bottom of the tallest cell in your row.
Hope this helps!
Example:
https://jsfiddle.net/b9chris/njcnex83/embedded/result/
Adapted from several answers here. The flexbox-based answers are the right way once IE8 and 9 are dead, and once Android 2.x is dead, but that is not true in 2015, and likely won't be in 2016. IE8 and 9 still make up 4-6% of usage depending on how you measure, and for many corporate users it's much worse. http://caniuse.com/#feat=flexbox
The
display: table
,display: table-cell
trick is more backwards-compatible - and one great thing is the only serious compatibility issue is a Safari issue where it forcesbox-sizing: border-box
, something already applied to your Bootstrap tags. http://caniuse.com/#feat=css-tableYou can obviously add more classes that do similar things, like
.equal-height-md
. I tied these to divs for the small performance benefit in my constrained usage, but you could remove the tag to make it more generalized like the rest of Bootstrap.Note that the jsfiddle here uses CSS, and so, things Less would otherwise provide are hard-coded in the linked example. For example @screen-sm-min has been replaced with what Less would insert - 768px.
I use this super easy solution with
clearfix
, which doesn't have any side effects.Here is an example on AngularJS:
And one more example on PHP:
Update 2018
Best approach for Bootstap 3.x -- using CSS flexbox (and requires minimal CSS)..
Bootstrap same height flexbox example
To only apply the same height flexbox at specific breakpoints (responsive), use a media query. For example, here is
sm
(768px) and up:This solution also works well for multiple rows (column wrapping):
https://www.bootply.com/gCEXzPMehZ
Other workarounds
These options will be recommended by others, but are not a good idea for responsive design. These only work for simple single row layouts w/o column wrapping.
1) Using huge negative margins & padding
2) Using display:table-cell (this solution also effects the responsive grid, so a @media query can be used to only apply
table
display on wider screens before the columns stack vertically)Bootstrap 4
Flexbox is now used by default in Bootstrap 4 so there is no need for the extra CSS to make equal height columns: http://www.codeply.com/go/IJYRI4LPwU
To answer your question this is all you need see full responsive demo with prefixed css:
To add support for thumbnail content flex within flex columns like the screenshot above also add this... Note you could do this with panels as well:
While flexbox doesn't work in IE9 and below you can use the demo with a fallback using conditional tags with something like javascript grids as a polyfill:
As for the other two examples in the accepted answer... The table demo is a decent idea but is being implemented wrong. Applying that CSS on bootstrap column classes specifically will without a doubt break the grid framework entirely. You should be using a custom selector for one and two the tables styles should not be applied to
[class*='col-']
that have defined widths. This method should ONLY be used if you want equal height AND equal width columns. It is not meant for any other layouts and is NOT responsive. We can make it fallback however on mobile displays...Lastly, the first demo in the accepted answer which implements a version of the one true layout is a good choice for some situations, but not suitable for bootstrap columns. The reason for this is that all the columns expand to the container height. So this will also break responsiveness since the columns are not expanding to the elements next to them, but the entire container. This method will also not allow you to apply bottom margins to rows any longer and will also cause other issues along the way like scrolling to anchor tags.
For the complete code see the Codepen which automatically prefixes the flexbox code.