Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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
      </div>
    </div>
    

    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:

    1cdT7CL

    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:

    qgcOvyK

    ?

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

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

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

    Did you enjoy this article?

    Continue reading about The one thing flexbox can't do

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 by Swizec with ❤️