Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,109 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