Over a million developers have joined DZone.

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

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
css ,flexbox ,design ,swizec teller

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}