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
Alex Weinberg
2,726 PointsCan't center nav.
I'm having trouble with this. I really just want my nav centered in the browser window. Any suggestions? Thanks.
<ul id="main-nav">
<a href="#videos"><li>Home</li></a>
<a href="#videos"><li>Media
<ul id="sub-nav">
<a href="#videos"><li>Videos</li></a>
<a href="#photos"><li>Photos</li></a>
</ul>
</li></a>
<a href="#about"><li>About
<ul id="sub-nav" >
<a href="#band"><li>Band</li></a>
<a href="#players"><li>Players</li></a>
</ul>
</li></a>
<a href="#contact"><li>Contact</li></a>
</ul>
<div class="nav-button"></div>
* {
box-sizing: border-box;
}
a {
text-decoration: none;
color: white;
display: block;
}
ul {
width: 100%;
display: block;
padding: 0;
margin: 0 auto;
}
li {
width: 20%;
list-style: none;
padding: 5px 0;
float: left;
background-color: grey;
text-align: center;
font-size: 1.3rem
}
#main-nav {
margin: 0 auto;
display: block;
}
#main-nav li {
margin-right: .5%;
border-radius: 4px;
}
#sub-nav {
width: 100%;
display: none;
padding-top: 6px;
text-align: center;
}
#sub-nav li {
border-radius: 0 0 2px 0;
clear: left;
width: 100%;
text-align: center;
border-style: solid;
border-width: 2px 0 0 0;
}
#main-nav li:hover #sub-nav {
display: block;
}
.nav-button {
display: none;
width: 30px;
height: 30px;
}
@media screen and (max-width: 660px) {
#main-nav {
display: none;
}
.nav-button {
display: inline-block;
}
}
1 Answer
Mark Truitt
17,230 PointsGreetings Alex,
There are a few things wrong here.
I went ahead and modified some of the minor stuff and made it work how you were asking. However, there is still some tweaking to do.
The anchor tag should not be wrapped around the list item. I also went ahead and adjusted some of the css to make it work how you were asking but left other things that still needed to be changed to make it look how you started.
Responsively, the below code works till your media query hides it. Using a set width like the above will cause issues on responsive as the viewport gets smaller.
<ul id="main-nav">
<li><a href="#videos">Home</a></li>
<li><a href="#videos">Media</a>
<ul id="sub-nav">
<li><a href="#videos">Videos</a></li>
<li><a href="#photos">Photos</a></li>
</ul>
</li>
<li><a href="#about">About</a>
<ul id="sub-nav" >
<li><a href="#band">Band</a></li>
<li><a href="#players">Players</a></li>
</ul>
</li>
<li><a href="#contact">Contact</a></li>
</ul>
<div class="nav-button"></div>
* {
box-sizing: border-box;
}
a {
text-decoration: none;
color: white;
display: block;
}
ul {
padding: 0;
margin: 0;
text-align: center;
}
li {
display: inline-block;
list-style: none;
padding: 5px 10px;
background-color: grey;
text-align: center;
font-size: 1.3rem
}
#main-nav {
margin: 0 auto;
display: block;
}
#main-nav li {
margin-right: .5%;
border-radius: 4px;
position: relative;
}
#main-nav li:last-child {
margin: 0;
}
#sub-nav {
width: 100%;
display: none;
padding-top: 6px;
text-align: center;
}
#sub-nav li {
}
#main-nav li:hover #sub-nav {
display: block;
position: absolute;
padding: 0;
left: 0;
top: 100%;
}
.nav-button {
display: none;
width: 30px;
height: 30px;
}
@media screen and (max-width: 660px) {
#main-nav {
display: none;
}
.nav-button {
display: inline-block;
}
}
Mark