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 Layout Basics CSS Layout Project Adding Media Queries for Large Screens

Gabriel Ward
Gabriel Ward
20,222 Points

Centering the main-nav

In the video, Guil floats the main-nav to the right. I'm curious to know what the best way to center the main-nav would be. Is it possible to use float on the main-nav and center it?

2 Answers

Hi Gabriel,

You can use margin to center an element within its container providing it has width that is not set to auto, and the child width is not set to say 100% filling the parent container.

.my-nav {
    margin: 0 auto;
}

As basic as this may seem if the parent element styles were as follows:

.my-nav-container {
   width: 100%;
}

No floats needed and no worrying about clearing floats either.

You should be good to go :)

Hope this helps

Craig

Gabriel Ward
Gabriel Ward
20,222 Points

Hi Craig,

Thanks. And in this case here with this project, to display the ul would beneath the h1, ul element would need to be moved into it's own container div separate from the div with the h1 class='name' right?

So we'd change

<div class="container clearfix">
<h1 class="name"><a href="#">Best City Guide</a></h1>
<ul class="main-nav">
<li><a href="#">ice cream</a></li>
<li><a href="#">donuts</a></li>
<li><a href="#">tea</a></li>
<li><a href="#">coffee</a></li>
</ul>
</div>

to something like,

<div class="container clearfix">
<h1 class="name"><a href="#">Best City Guide</a></h1>
</div>
<div class='container clearfix>
<ul class="main-nav">
<li><a href="#">ice cream</a></li>
<li><a href="#">donuts</a></li>
<li><a href="#">tea</a></li>
<li><a href="#">coffee</a></li>
</ul>
</div>
Robert Szabo
Robert Szabo
7,999 Points

Hey Gabriel,

I don't think so you need to move anything, since the ul , h1 , div are all block-level elements.