Flexbox Equal Height Columns

By  on  

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!

Recent Features

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    Detect DOM Node Insertions with JavaScript and CSS Animations

    I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API.

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Discussion

  1. Flexbox : so great and so easy to use. Wonderful !

  2. Ahh, this is helpful! Awesome and marvelous explanation

  3. That is really useful! I recently started doing web development and had to do just this.

  4. Tune

    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)

    • John

      Just remove the fixed min-height of the example and you’ll see that the two columns remain of equal height.

  5. Thanks for the post, I am going to start using this a lot now.

  6. Adam

    Already using it almost everywhere I can. Syntax is also really easy to remember. Easy and powerful.

  7. 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

    display: inline-block;

    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.

  8. Mephysto

    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.

  9. shirahara

    How about just using the columns property?
    https://developer.mozilla.org/en-US/docs/Web/CSS/columns

    .foo {
        columns: 2;
        column-gap: 20px;
    }
    
  10. MJR

    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.

  11. 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.

  12. Jan

    How do I add vertical lines between the columns to visual separate them ?

    • Add a border-left to the second column DIV

  13. Mahesh Waghmare

    But, What for 3 divs. Can we create 50% column grid for 3 or more flex items using flex parent.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!