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:
Assuming an HTML structure that looks like this:
Sure beats the best hack from a few years ago: three divs with 50% offset positioning. The only thing harder for pure CSS than vertical centering is a three-column design with equal-height columns. A good hack was never found.
Want three columns that stretch proportionally to your spec? Say no more.
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:
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. Ideally, you could loop through them, make a list of buttons in HTML, and have CSS take care of layout.
flex-flow: row wrap is your friend. It breaks elements into lines and looks like this:
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 row span. You’ll have to suck it up and put your things into column elements.
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 row span. Not painful at all.