Over a million developers have joined DZone.

The One Thing Flexbox Can’t Do

DZone's Guide to

The One Thing Flexbox Can’t Do

Flexbox was going to solve everything. Vertical centering. No more floats. Columns made easy! But Swizec Teller found its Achilles' heel — row spans.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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

Until Flexbox!

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

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

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 three-row flexbox by Swizec Teller (@swizec) on CodePen.0

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:

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

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.

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

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.

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

css ,flexbox ,design ,swizec teller

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}