Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css Flexbox Navbar Right Align

Tags:

html

css

flexbox

basically what I am trying to do is to implement a navigator in css without using float/clearfix. Instead I am using a better approach, css3's flexbox. I know it's simple to align items to the left easily but what's the optimal solutions for having a narbar which has two aligned regions ?

Something like:

[Home]-[Stuff]------------[Credits]-[About]

This was my attempt implementing this:

https://jsfiddle.net/asss321/ornontbt/

Basically the idea is to wrap two flexboxes into one and set the right flexbox to:

.child.child-right {
    flex-grow: 1;
    justify-content: flex-end;
}

Is this a good way to do it ? Or I am using some hacky way ?

like image 293
v4ss Avatar asked Oct 24 '25 06:10

v4ss


1 Answers

Your way is not a bad one. However, using nested elements may be unnecessary.

.container {
  display: flex;
  border: solid 1px black;
}
.container > :nth-child(2) {
  margin-right: auto;
}
a {
  padding: 10px;
}
<div class="container">
  <a href="#">Start</a>
  <a href="#">Extra</a>
  <a href="#">Credits</a>
  <a href="#">About</a>
</div>
like image 162
Oriol Avatar answered Oct 25 '25 21:10

Oriol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!