I am totally new to Flexbox and wanted to align buttons, but I could not see how to handle the common case with a center-aligned button and a right-aligned button on the same row using only Flexbox.
However, I found a way that used an invisible left-aligned item of the same length as the right-aligned item and the flex justify-content
with space-between
to make the middle item centered on the row.
Is there a more direct way with Flexbox?
.flexcontainer { display: flex; justify-content: space-between; width: 500px; height: 200px; } .iteminvisible { flex: 0 1 auto; width: 100px; height: 100px; visibility: hidden; } .itemcenter { flex: 0 1 auto; width: 150px; height: 100px; } .itemright { flex: 0 1 auto; width: 100px; height: 100px; }
<div class="flexcontainer"> <div class="iteminvisible">Other</div> <div class="itemcenter">One</div> <div class="itemright">Other</div> </div>
By using align-items: center we can vertically center all flex items to the parent container along the cross axis of the flex container. The nice aspect of flexbox is the styles apply to all children of our flex container. If we add two more elements they all stay centered within the parent select.
You can also align a flex item to the right by setting the CSS margin-right property. In our example below, we set the "auto" value of this property on the "align2" class.
If you just want specific flex items to be centered vertically, you can set align-self on the items instead: arrr! yeehaw! If you want to put an element at the dead center of a page, it can be a little bit more tricky because flex items will only center vertically according to the height of their container.
How to center a <div> using Flexbox property of CSS ? 1 We use the property of display set to flex i.e. display: flex; 2 Align items to center using align-items: center; 3 The last step is to set justify-content to center i.e. justify-content: center;
Using justify-content: space-between
with an invisible flex item, as described in your question, is a good way to achieve the layout you want. Just note that the middle item can only be centered if both left and right items are equal length (see demo).
Another solution you may want to consider involves auto
margins and absolute positioning. Two benefits of this method are no need for extra mark-up and true centering can be achieved regardless item sizes. One drawback is that the centered item is removed from the document flow (which may or may not matter to you).
.flexcontainer { display: flex; justify-content: flex-start; /* adjustment */ position: relative; /* new */ width: 500px; height: 200px; } .itemcenter { flex: 0 1 auto; width: 150px; height: 100px; position: absolute; /* new */ left: 50%; transform: translateX(-50%); } .itemright { flex: 0 1 auto; width: 100px; height: 100px; margin-left: auto; /* new */ }
<div class="flexcontainer"> <div class="itemcenter">One</div> <div class="itemright">Other</div> </div>
More details here: Methods for Aligning Flex Items along the Main Axis (see boxes #62-78).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With