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

help with nav bar!

I am trying to create a nav bar where my logo is in the middle and there are nav buttons on both sides, however, I seriously have no clue as how to accomplish this.

1 Answer

Cosmin Cioaclă
Cosmin Cioaclă
21,339 Points
<style>
    .navigation-left,
    .navigation-logo,
    .navigation-right {
        float: left;
        width: 33.33%;
    }

    .logo {
        margin: 0 auto;
    }
</style>

<nav>
    <div class="navigation-left">   
        <ul>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
        </ul>
    </div>

    <div class="navigation-logo">
        <div class="logo">
            <a href="/"><img src="logoimage.jpg" alt="logoimage"></a>
        </div>
    </div>

    <div class="navigation-right">
        <ul>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
        </ul>   
    </div>
</nav>

Try this out.

Lucas Krause
Lucas Krause
19,924 Points

To make margin:0 auto; working you have to specify a width for .logo, or am I wrong?
But since <img> is an inline element you can simply leave off the .logo <div> and add the following CSS:

.navigation-logo {
    text-align:center;
}
Cosmin Cioaclă
Cosmin Cioaclă
21,339 Points

You are right. The width needs to be specified.

text-align: center should work too.