Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Flexbox Layout Building a Layout with Flexbox Building a Navigation Bar with Flexbox

Frank Book
Frank Book
308 Points

3 elements solution

I am trying to figure out how to work with 3 elements.

1st is the company logo

2nd ul with navigations

3rd is button

  <header class="main-header">
    <img class="item-0"src="" alt="">
    <ul class="main-nav">
       <li class="item-1"><a href="#">Home</a></li>
       <li class="item-2"><a href="#">Gallery</a></li>
       <li class="item-3"><a href="#">Locations</a></li>
       <li class="item-4"><a href="#">Contact Us</a></li>
    </ul>
    <p class="call-button"><a href="#">Call Now</a></p>
  </header>

This is flexbox

.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.main-nav {
  display: flex;
  gap: 20px;
}

.call-button {
  margin-left: auto;
}

I tried this in flex box which got me closer, but it's not perfect. Keeps the logo on the left side, and the button on the right side which is great, but nav items should be close to the button and spread from right to all the way to the centre of the user screen.

Thank you for your response

Think about how you got the button to the right and maybe try that out with the main-nav ul?

Then, if you find the button and the main nav are still not as close as you would like, right click the button and inspect item. check out the margin on the button. If the margin: auto is causing the button to push main nav to far away, think about ways to get the button to sit to the right side with the margin-right property. If margin-right: auto would attempt to push the button as far left as possible, what might margin-right: 0px do?

Check it out.

1 Answer

Steven Parker
Steven Parker
231,269 Points

By default, the list (ul) only takes as much space as it needs, so there's no room to stretch out. Also, it needs its own justify-content to make it do so. So you might do something like this:

.main-nav {
  display: flex;
  flex: auto;
  justify-content: space-around;
}