Flexbox Equal Height Columns
Flexbox was supposed to be the pot of gold at the long, long rainbow of insufficient CSS layout techniques. And the only disappointment I've experienced with flexbox is that browser vendors took so long to implement it. I can't also claim to have pushed flexbox's limits, but the technique has allowed me to accomplish a few tasks which were overly complicated in the past. One task was easily mastering vertical centering with flexbox.
My next task was creating a responsive two-column layout with columns of equal width, equal height, and fixed-pixel margin between them, as well as any amount of padding. It was appallingly easy...and that's why I love flexbox!
The HTML
The markup requires one parent and two child elements:
<div class="flexbox-container"> <div><h3>Column 1</h3></div> <div><h3>Column 2</h3></div> </div>
The content in each column can be any height -- that's not important here.
The CSS
The CSS is also incredibly easy and brief:
.flexbox-container { display: -ms-flex; display: -webkit-flex; display: flex; } .flexbox-container > div { width: 50%; padding: 10px; } .flexbox-container > div:first-child { margin-right: 20px; }
Simply set the display to flexbox on the parent and then give each child 50% width. What's sweet about flexbox is you can add padding, border, and margin to the child elements without needing to worry about a column spilling over to the next row.
I was super excited when I learned about CSS calc because I wanted to shim what flexbox does today, but now that flexbox is supported by just about every modern browser, I don't need CSS calc for layouts. Excellent! I'm so glad that flexbox is here -- tasks that should be are easy now!
Flexbox : so great and so easy to use. Wonderful !
Ahh, this is helpful! Awesome and marvelous explanation
That is really useful! I recently started doing web development and had to do just this.
Nice, but the height is now fixed at 200px. How can I achieve equal height of both divs without knowing the contents of each div on beforehand? (f.i. user generated content) (Currently I solved it with JS, but I hope to find a pure CSS solution)
Just remove the fixed
min-height
of the example and you’ll see that the two columns remain of equal height.Thanks for the post, I am going to start using this a lot now.
thanks luke
Already using it almost everywhere I can. Syntax is also really easy to remember. Easy and powerful.
Thanks for the post, it once more shows that flexbox layout is a viable option these days.
However, when I built a responsive website for a streaming provider lately, I stumbled upon some incompatibilities concerning older Android smartphones, especially with Android’s system browser:
While flexbox would have been great, considering one of the design requirements was a multi column layout, it unfortunately didn’t work out on every device, so I had to use
in conjunction with Modernizr to get the layout to at least work somehow on older smartphones. I consider this kind of a stumbling block, seeing that – at least here in Germany – older Android smartphones are still around.
I just ban those browsers (and show a “please upgrade” message) using UA matching.
Awesome!
When can we use it? (Hint: it’s when we can drop support for IE9. Which is at least 5 more years away). Or I am missing some awesome polyfill which automagically replaces it to clearfix and floats or something.
How about just using the
columns
property?https://developer.mozilla.org/en-US/docs/Web/CSS/columns
Flexbox is a good concept but still has a way to go due to browser issues and other niggly problems. IE isn’t fully compatible with the other browsers and needs quirky CSS code to make it conform. Animated sprites don’t work properly in any of the browsers just now. Check out https://github.com/philipwalton/flexbugs/issues/ for more information.
Simple and elegant solution for flex two col layout. I tried several methods but I over complicated things. Thanks to this post I found the answer I was looking for. Good One.
How do I add vertical lines between the columns to visual separate them ?
Add a
border-left
to the second columnDIV
But, What for 3
div
s. Can we create50%
column grid for 3 or more flex items using flex parent.What happens if you have three columns inside the flexbox-container with width set to 33.3333%?
Thanks for the blog post David! I always find your posts to be helpful and informative.