Wednesday, 18 April, 2018 UTC


I was one of the biggest fans of flexbox before it hit but, due to being shuffled around at Mozilla, I never had the chance to use it in any practice project; thus, flexbox still seems like a bit of a mystery to me.  This greatly pains me because I feel left out of the flexbox revolution, though these days the sentiment I hear is that flexbox turned out to be a bust and that CSS grid is the new hotness.
One awesome utility of flexbox is spacers.  One problem of the past was docking items to the left, items to the right, but needing to mess around with absolute positioning to make it happen.  With flexbox it’s easy to accomplish this effect without needing fixed element sizes or JavaScript width calculations.
Let’s assume a very simple structure of a parent, a left and right child, and a spacer:
<div class="parent">
  <div class="left-buttons"></div>
  <div class="spacer"></div>
  <div class="right-buttons"></div>
The spacer will be empty.
The CSS will be flexbox-based:
.parent {
  display: flex;

.spacer {
  flex-grow: 1;
The key is having a parent with display: flex and the spacing having flex-grow: 1.  Regardless of how wide the left and right get, the spacer takes up the remaining space.  If the left and right grow larger than the available space…then things get pushed around.
See the Pen Flexbox Spacers by David Walsh (@darkwing) on CodePen.
Flexbox gets a bad rap — the layout APIs of old were so bad that a moderate upgrade should still be celebrated.  Regardless, let’s celebrate a simple API that allows us to create this sample layout that we’ve wanted for so long.
The post Creating Spacers with Flexbox appeared first on David Walsh Blog.