Creating Spacers with Flexbox
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.
The HTML
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> </div>
The spacer will be empty.
The CSS
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.
Isn’t that what
justify-content: space-between
is for? So you don’t need spacer divs? Otherwise, I think you can set a right margin on the left buttonsdiv
or left margin on the right buttons to the value ofauto
and achieve the same thing…For this simple case you don’t even need the spacer element. Just add
justify-content: space-between;
on the parent.You can also use
justify-content: space-between
so you don’t have to use a spacer element.Personally I’d reach for margin left or right Auto on either the first or second element. But I agree that flexbox is super cool!
Hi David (great name! ;)), with flexbox, you don’t need a spacer, just add
margin-left: auto
to the right button and its going to fill in the blank space, here is the spec – https://www.w3.org/TR/css-flexbox-1/#auto-marginsNo need for a spacer.
Solution 1.
https://codepen.io/hudochenkov/pen/Vxwjbv
Solution 2.
on the last item:
https://codepen.io/hudochenkov/pen/YLzWrq
Hi David. You don’t need the spacer element to achieve this justification as there’s
justify-content: space-between;
, applied to the flex container. https://css-tricks.com/snippets/css/a-guide-to-flexbox/ is very helpful.What about the
justify-content
property? There is no need for the empty.spacer
if you set it tospace-between
.Uhm… but why the need for an empty spacer div in this situation while there is ‘justify-content: space-between’?
Empty divs are bad.
This can be accomplished with
justify-content: space-between
on the parent: https://codepen.io/anon/pen/gzOGQpDon’t rush posts on something you just started with ;)
With all do respect, I’m glad David “rushed” this post. I quickly learned a little something about flexboxes as well as the justify-content attribute. Thank you both!
An alternative approach would be to apply
margin-left: auto
to the.right-buttons
element *instead* of using a spacer element:https://codepen.io/simevidas/pen/KRKXGm?editors=1100
Hi David.
There is no need to create a divs at all. Solution no 1:
… and solution no 2:
You can make spacer with pseudo-elements too, without any extra div
(forgot the
content: ""
in my last comment )