Swizec Teller - a geek with a hatswizec.com

    The one thing flexbox can't do

    Ah yes, flexbox – that marvel of stylesheet technology that was going to absolve us, make our lives easier, and leave enough time for beers after work. How wonderful our future was to be!

    Imagine it: no more floats! No more strange CSS incantations with negative margins just to get some basic layout working! All those tricks you spent the last 10 years learning? Forget them. You don’t need them.

    Flexbox is here to save you!

    Want to center vertically in a container of unknown height? Flexbox can do it with a line of code! And semantic HTML.

    With flexbox, vertical centering looks like this:

    .container {
    display: flex;
    .centered {
    flex: 1;

    Assuming an HTML structure that looks like this:

    <div class="container">
    <div class="centered">
    This is centered

    Sure beats the best hack from a few years ago: 3 divs with 50% offset positioning. 1 The only thing harder for pure CSS than vertical centering is a 3-column design with equal-height columns. A good hack was never found.

    Until Flexbox!

    Want 3-columns that stretch proportionally to your spec? Say no more.

    See the Pen 3-column flexbox by Swizec Teller (@swizec) on CodePen.

    Simple, isn’t it? Set the container up as a flexbox, then tell each child div to take up an equal amount of space. Open it on Codepen, and play around with screen size. It all works.

    You can even turn it into rows without changing HTML. Like this:

    See the Pen 3-row flexbox by Swizec Teller (@swizec) on CodePen.

    Row span in flexbox

    But then your designer comes up with this:


    It’s a grid of same-size buttons, and you are boned.

    You know the buttons have to be configurable, which means your code should be agnostic to their configuration. There can be more or less of them, their labels can vary, and their colors are configurable, too.

    Those buttons are data. You get an array of labels, and you have to render them as buttons.2 Ideally, you could loop through them, make a list of buttons in HTML, and have CSS take care of layouting.

    flex-flow: row wrap is your friend. It breaks elements into lines and looks like this:

    See the Pen 6-grid flexbox by Swizec Teller (@swizec) on CodePen.

    That’s a grid – wasn’t hard at all. But your designer wants that first cell to span two rows.

    Flexbox row span. Now you’re boned. If you take the naive approach like I did, the result looks like this:



    Maybe they won’t notice?

    Seriously, though, you can’t do this. There’s no such thing as a flexbox rowspan. You’ll have to suck it up and put your things into column elements.

    See the Pen 6-grid flexbox by Swizec Teller (@swizec) on CodePen.

    Wrap each group of elements into another div, set that div to display: flex, tell it to flex-direction: column, and make sure one of the elements contains only two elements. Give those elements different flex weights.

    Voila, flexbox rowspan. Not painful at all. ?

    1. Many methods for vertical centering exist, but this has always been a panacea of CSS layouting. The hardest, most impossible thing to do. Google “CSS vertical centering” if you don’t believe me. ↩︎

    4) The real array has more than just labels, but let’s keep it simple. ↩︎


    Did you enjoy this article?

    Published on September 7th, 2016 in CSS, flexbox, HTML, Technical, Web Design and Development

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️