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

Shaked Gvirtsman
Shaked Gvirtsman
2,270 Points

Help centering nav

I'm trying to center my navigation menu and just can't.... I tried many thing I found on the web but nothing is working. This is my html file:

<!DOCTYPE html>
<html>
        <head>
            <title>shaked</title>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
    <nav>
      <ul id="navigation-bar">
        <li>About Me</li>
        <li>Lectures</li>
        <li>gallery</li>
        <li>contact me</li>
      </ul>
      </nav>
        <header>
        <h1>Welcome to my website</h1>
        </header>
    </body>
</html>         
    here is my CSS code:
#navigation-bar li {
  color: green;
  display: inline;
 padding: 0px 15px 5px;
  margin-right: 10px;
 background-color: lightblue;
}
nav {
  margin-left: auto;
  margin-right: auto;
}

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

You were quite close! Keep working on those box-models to get a better understanding of what's going on. This type of problem is a prime candidate to flexbox!

To do this in flexbox, all you'd need is this

#navigation-bar {
   display: flex;
  justify-content: center;
}

But, that's not what you were asking, so by turning the list items into inline-block, and setting the nav to text-align: center, we are able to accomplish this.

Here's your example, that I've modified: https://codepen.io/anon/pen/pxjQEO