views:

237

answers:

1

I have a list that I'd like the main elements to align vertically and the sub elements of each to drop down underneath the main element. I want to keep the position: absolute on the subNav class because the width of this nav will vary from each so I can't set a width. This works in Firefox, but in IE 7 the absolute causes the subnav to display inline (so shifted to the right and up from where I would like). How can I fix this?

Example:

<style>
#nav ul, #nav li ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
#nav li {
    float: left; 
    width: 120px;
    border-right: 1px solid #000;
    padding: 10px;
}
#nav li ul li {
    float: none; 
    width: auto;
    height: auto;
    border-right: none;
    text-align: left;
    padding: 0; 
}

#nav .subNav {  
    background: #eee; 
    position: absolute;
    padding: 10px;
}
</style>
</head>

<body>
<div id="nav">
    <ul>
    <li>Main One    
            <ul class="subNav">
     <li>Sub One A</li>
     <li>Sub One B</li>
        </ul>   
    </li>
    <li>Main Two 
        <ul class="subNav">
     <li>Sub Two A</li>
     <li>Sub Two B</li>
        </ul>
    </li>
    </ul>
</div>
</body>
+1  A: 

Don't forget to put in your top and left values.

nav .subNav{
    top:10px;
    left:20px;
}

nav.containerDiv {
     position:relative;
}

HTML

<ul class="nav">
   <li>
      <div class="containerDiv">
         <ul class="subNav">...
      </div>
   </li>
</ul>

This will result in the subNav being relative to the container div, instead of the whole document.

Zoidberg
But if I put a left position on the subNav, which has absolute positioning, then all of the subNav items will appear vertically in the same place which I do not want. I want them to appear directly under their Main Item counterpart.
Then put a div around the sub nav list and make that div have a position of relativeposition:relative;and make sure that your sub nav div is inside of that div. What css will do is make the positioning absolute to the first parent element with position relative or position absolute, in this case, your new div. I will edit the response to show this
Zoidberg