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:
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.
Want 3-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:
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:
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.
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. ?
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. ↩︎
The real array has more than just labels, but let’s keep it simple. ↩︎
Continue reading about The one thing flexbox can't do
Semantically similar articles hand-picked by GPT-4
- Firefox's "funny" css3 image scaling quirk
- Build responsive SVG layouts with react-svg-flexbox
- Why CSS-in-JS is winning, an example
- How to tell a phone your website is an app
- Backbone with ES6
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Join Swizec's Newsletter and get insightful emails 💌 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. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn 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
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
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️