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

Nav menu background covering too much

It's covering all the header area when it should only be covering the menu/nav area.

www.sunnysihota.com

here's the css code:

 /* Main */

menu{

width: 100%;
margin: 0;
padding: 10px 0 0 0;
list-style: none;  
background: #111;
background: -moz-linear-gradient(#444, #111); 
    background: -webkit-gradient(linear,left bottom,left top,color-stop(0, #111),color-stop(1, #444));  
background: -webkit-linear-gradient(#444, #111);    
background: -o-linear-gradient(#444, #111);
background: -ms-linear-gradient(#444, #111);
background: linear-gradient(#444, #111);

-moz-box-shadow: 0 2px 1px #9c9c9c;
-webkit-box-shadow: 0 2px 1px #9c9c9c;
box-shadow: 0 2px 1px #9c9c9c;

}

menu li{

float: left;
padding: 0 0 10px 0;
position: relative;

}

menu a{

float: left;
height: 25px;
padding: 0 25px;
color: #999;
text-transform: uppercase;
font: bold 12px/25px Arial, Helvetica;
text-decoration: none;
text-shadow: 0 1px 0 #000;

}

menu li:hover > a{

color: #fafafa;

}

html #menu li a:hover{ / IE6 */ color: #fafafa; }

menu li:hover > ul{

display: block;

}

html #menu ul a{ / IE6 */
height: 10px; width: 150px; }

:first-child+html #menu ul a{ / IE7 */
height: 10px; width: 150px; }

menu ul a:hover{

    background: #0186ba;
background: -moz-linear-gradient(#04acec,  #0186ba);    
background: -webkit-gradient(linear, left top, left bottom, from(#04acec), to(#0186ba));
background: -webkit-linear-gradient(#04acec,  #0186ba);
background: -o-linear-gradient(#04acec,  #0186ba);
background: -ms-linear-gradient(#04acec,  #0186ba);
background: linear-gradient(#04acec,  #0186ba);

}

menu ul li:first-child a{

-moz-border-radius: 5px 5px 0 0;
-webkit-border-radius: 5px 5px 0 0;
border-radius: 5px 5px 0 0;

}

menu ul li:first-child a:after{

content: '';
position: absolute;
left: 30px;
top: -8px;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 8px solid #444;

}

menu ul li:first-child a:hover:after{

border-bottom-color: #04acec; 

}

menu ul li:last-child a{

-moz-border-radius: 0 0 5px 5px;
-webkit-border-radius: 0 0 5px 5px;
border-radius: 0 0 5px 5px;

}

/* Clear floated elements */

menu:after{

visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;

}

* html #menu             { zoom: 1; } /* IE6 */
*:first-child+html #menu { zoom: 1; } /* IE7 */

4 Answers

Christopher Myers
Christopher Myers
8,595 Points

Elements that come after a floated element will wrap around the floated element unless you clear them. Every element above your navigation menu was floated, so your menu wrapped around them.

Khemraj Thapa
Khemraj Thapa
8,885 Points

add display:inline-block; to your #menu, its seems to work.. or simply clear the floating elements by clear:both; it seems to work as well.. im not expert but i just played around and its seems to work. :D

Christopher Myers
Christopher Myers
8,595 Points

Some versions of IE don't like inline-block. Clearing the floats by adding "clear:both;" to your menu ID does the trick. Also, don't be afraid to use some HTML 5 tags. You declared HTML 5 with your doctype so feel free to replace some of those divs.

I'm not sure why clear: both; works, maybe you can share that with me.

thanks for the help, it worked.

The clear property specifies which sides of an element where other floating elements are not allowed so by setting it to clear both it is making sure that any elements that are floating close by in the html will not be able to sit on the left and right sides of the element with clear:both assigned forcing the element to sit below the floating elements.