I have designed a Navigation Menu, it working properly on Chrome, Firefox and IE8.
It appear it not working properly on IE7, the margin and the size of the <li> is too small, sub-menu is not appearing in full and also #background is missing.
It should look the same as Chrome and Firefox.
See example: http://jsfiddle.net/FFWfp/
How to fix this?
HTML
<div id="background">
<div class="nav-block">
<ul id="nav">
<li><a class="active" href="/">Home</a></li>
<li>
<a href="/">Category</a>
<ul class='subnav'>
<li><a href='#'>PHP </a></li>
<li><a href='#'>HTML</a></li>
<li><a href='#'>CSS</a></li>
</ul>
</li>
<li>
<a href="/">Account</a>
<ul class='subnav'>
<li><a href='#'>One</a></li>
<li><a href='#'>Two</a></li>
<li><a href='#'>Three</a></li>
</ul>
</li>
<li><a href="/admin/reports">Logout</a></li>
</ul>
</div>
</div>
CSS
<style>
#background {
background-color:#EBE9E1;
overflow:hidden;
}
.nav-block{
background-color:black;
height:50px;
}
#nav {
padding:12px;
list-style:none;
}
#nav li{
display:inline;
margin:0 1px 0 -1px;
padding:3px 15px;
float:left;
font-size:14px;
}
#nav a {
background-color:white;
color:#C51721;
padding:10px;
text-decoration: none;
}
#nav .subnav {
padding:0px;
list-style:none;
width:130px;
border-right: 2px solid black;
border-bottom: 2px solid black;
border-left: 2px solid black;
color:#000000;
margin-top:9px;
margin-left:-2px;
background-color:white;
}
#nav .subnav li {
padding:0px;
float: none;
width:100px;
color:#000000;
margin:0px;
}
#nav .subnav li a {
padding:3px;
padding-left:10px;
display:block;
background-color:white;
color:#C51721;
text-decoration: none;
border-bottom:1px solid #DEDEDE;
}
</style>
This should be simple, clearfix, containing floats whatever.. but it’s not This is a hasLayout error that compounds, it is one of the rare situations where you need to remove haslayout, but where it’s not possible to do so.
The way your code is written (nothing wrong with it by the way!) means the
#background divneeds hasLayout to contain the floated children (even trying an extra clearing element doesn’t work which shows the out and out IE error). Your code has this withoverflow : hidden;, but then because there’s hasLayout on the.nav-blockdue to the 50px height, that height, in IE7, is all that’s being "contained" – this is of course wrong, as the floated lists are children of#backgroundtoo.. however it’s because of an error that hasLayout even works to contain floats so there’s no use arguing the specs!I tried every hack I knew how and but the simplest way was to rewrite the code and avoid hasLayout where possible, and use margins and line-heights instead – so I reversed the coloring of the two containing blocks. I made the
backgroundblack and the.nav-blockgrey & gave the background a top padding of 50px to make the black bar.. then I moved the menu which is inside the grey bit up by 43px with a negative top margin. it’s 43px because in your example I measure the black bar @ 50px; and the top links @ 36px in height which meant to keep them looking vertically centered on the black bar they would have need50px-36px = 14px / 2 = 7pxtop & bottom ‘spacing’.the
.nav-blockdiv then needed to be the one that was made to contain the floated children, butoverflow: hiddenwon’t work now because of the negative top margin, it would hide the top links! so instead I floated it left and gave it a width of 100%; which is another way of creating a new block formatting context and giving IE the hasLayout it needs without clipping the content.then I pretty much followed what you had done to achieve the short top links and full width (130px) child links only floating the top links so they would "shinkwrap" – without the float on the child lists the child links could be made
display: blockso they take the full width of theul. For the height on the top links IE needed both line-height and padding, butline-heighton the child links was enough because of thedisplay: block;Example Fiddle
HTML is the same as yours above..
CSS: