Home > Net >  Using flexbox to make a navbar with 3 divs
Using flexbox to make a navbar with 3 divs

Time:01-17

I am trying to make a navigation bar that has three <div> elements, using flexbox layout; the desired look would have content on the far left, center, and far right. I just can't get them to separate from each other.

Here is my code:

* {
  padding: 0;
  margin: 0;
}

.navbar-wrapper {
  display: flex;
  flex-direction: row;
}

.left-navbar {
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: flex-start;
}

.middle-navbar ul {
  align-content: center;
  list-style: none;
  display: flex;
  justify-content: space-evenly;
  padding: 40px;
}

.right-navbar {
  align-items: center;
  justify-content: center;
}
<nav>
  <div >
    <div >
      <h1>Ben Resume</h1>
    </div>
    <div >
      <ul>
        <li><a href="#">Introduction</a></li>
        <li><a href="#">About Me</a></li>
        <li><a href="#">Reviews</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
    <div >
      <button type="button" name="Download CV">Download CV</button>
    </div>
  </div>
</nav>

CodePudding user response:

Add justify-content: space-between; to your .nabvar-wrapper

CodePudding user response:

I've also deleted unnecessary codes

* {
  padding: 0;
  margin: 0;
}

.navbar-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-around; /*or space-between*/
  align-items: center;
  width: 100%;
  padding: 20px 0;
}

.links {
  align-content: center;
  display: flex;
  justify-content: space-evenly;
}

.links a {
  margin-right: 10px;
}
<nav >
  <div >
    <h1>Ben Resume</h1>
  </div>
  <div >
    <a href="#">Introduction</a>
    <a href="#">About Me</a>
    <a href="#">Reviews</a>
    <a href="#">Contact</a>
  </div>
  <button type="button" name="Download CV">Download CV</button>
</nav>

CodePudding user response:

Use justify-content: space-between on your wrapper div.

CodePudding user response:

You must give the a width to .navbar-wrapper, and use justify-content: space-between for example`

.navbar-wrapper{
width:100%;
display:flex;
justify-content:space-between;

`

CodePudding user response:

There are actually two solutions when using flex:

Using space-between (from a parent perspective)

/* QuickReset */ *, ::after, ::before { margin: 0; box-sizing: border-box; }

.navbar-wrapper {
  display: flex;
  justify-content: space-between;
}
<div >
  <div>ONE</div>
  <div>TWO</div>
  <div>THREE</div>
</div>

or using margin auto (from a child perspective)

/* QuickReset */ *, ::after, ::before { margin: 0; box-sizing: border-box; }

.navbar-wrapper {
  display: flex;
}

.row-center {
  margin: 0 auto;
}
<div >
  <div>ONE</div>
  <div >TWO</div>
  <div>THREE</div>
</div>

  •  Tags:  
  • Related