Situation:
I have a simple nav bar that I'm building in Flexbox. I want to float one item to the left and keep the others pegged to the right.
Example:
<nav> <ul class="primary-nav"> <li><a href="#" id="item1">ListItem1</a></li> <li><a href="#" id="item2">ListItem2</a></li> <li><a href="#" id="item3">ListItem3</a></li> <li><a href="#" id="item4">ListItem4</a></li> </ul> </nav>
Problem
Typically answers involve just floating items left and right but supposedly in Flexbox it is bad to use Floats. I was thinking about using justify-content and using flex-start and flex-end but that Isn't working out too well.
I tried applying flex-start to the first item and then flex-end to the others but that didn't work so well.
Like So:
.primary-nav #item1 { justify-content: flex-start; } .primary-nav #item2 { justify-content: flex-end; } .primary-nav #item3 { justify-content: flex-end; } .primary-nav #item4 { justify-content: flex-end; }
Praise and thanks to anyone who has Flexbox skills and can help show me the proper way to handle this situation. :)
The flex columns can be aligned left or right by using the align-content property in the flex container class. The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container.
justify-content only works on flex-direction: row and flex-direction: row-reverse. In flex-direction: column you have to use align-items: flex-end;. You can test here and copy these code to your code.
To center our box we use the align-items property to align our item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally. You can take a look at the code of this example below.
To align one flex child to the right set it with margin-left: auto; From the flex spec: One use of auto margins in the main axis is to separate flex items into distinct "groups".
If you're looking to have just one element on the left and all others on the right, the simplest solution is to use justify-content:flex-end
on the parent element to move all elements to the right and then add margin-right:auto
to the element you want to have on the left
.primary-nav { display:-webkit-flex; display:flex; list-style-type:none; padding:0; justify-content:flex-end; } .left { margin-right:auto; }
<nav> <ul class="primary-nav"> <li class="left"><a href="#">ListItem1</a></li> <li class="right"><a href="#">ListItem2</a></li> <li class="right"><a href="#">ListItem3</a></li> <li class="right"><a href="#">ListItem4</a></li> </ul> </nav>
There is no need for margin hacks or pseudo-elements.
Simply create a division in your flex container and make the flex container justify by space between.
Inside the first division you put the items that will be on left, and inside the second division you put the items that will be on right.
nav.menu{ display: flex; justify-content: space-between; } nav.menu ul{ margin: 0; padding: 0; list-style: none; } nav.menu ul li{ display: inline-block; }
<nav class="menu"> <ul class="menu-left"> <li><a href="#">HOME</a></li> <li><a href="#">SERVICES</a></li> </ul> <ul class="menu-right"> <li><a href="#">MEMBERS</a></li> <li><a href="#">CONTACT</a></li> <li><a href="#">PROFILE</a></li> </ul> </nav>
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