tags:

views:

159

answers:

2

hi guys,

I'm trying to create a vertical menu and I need to make a multicolumn submenu. My code looks like this:

<style type="text/css" media="screen">


#nav {
     width: 100px;
    }
    #nav li {
     position: relative;
     background-color: red;
    }
    #nav li:hover .subnav {
     display: block;
    }
    .subnav {
     position: absolute;
     top: 0;
     left: 100px;
     display: none;
    }
    .subnav > li {
     float: left;
    }
</style>



<ul id="nav">
    <li>
     <a href="#">Link 1</a>
     <ul class="subnav">
      <li>
       <ul>
        <li>Col 1.1</li>
        <li>Col 1.2</li>
       </ul>
      </li>
      <li>
       <ul>
        <li>Col 2.1</li>
        <li>Col 2.2</li>
       </ul>
      </li>
      <li>
       <ul>
        <li>Col 3.1</li>
        <li>Col 3.2</li>
       </ul>
      </li>
     </ul>
    </li>
</ul>

My question is that why .subnav > li doesn't float left? I don't want to set width attribute, because I don't know how long the submenu item will be.

+1  A: 

Is this a viable workaround:

.subnav ul li {
    display: inline;
}

(The hover doesn't seem to work in IE if that's a concern).

opello
no, that didn't work. i'm using ff3.5 and safari 4 on a mac and i'm not worried about ie for now.
Baha
+5  A: 

I think the reason it's not working is because of this part of the CSS:

#nav {
    width: 100px;
}

This doesn't leave the sub-nav any horizontal room, so all of the floats wrap to the next line. Removing the width from #nav appears to let it do the right thing. If you do this, you'll have to make sure that the top-level menu items' text doesn't exceed 100 pixels in length; otherwise, the sub-nav will overlap it.

(it still doesn't work in IE, though).

Jacob
you are right, but if I remove #nav's width then it means it's width will be equal to it's parent's width (i.e. body width). However, in my site this #nav is located inside a dive with a width of 100px. If I take this #nav outside the div then the whole layout will be a mess.
Baha
Then I don't see a way around setting the width on the sub-nav. If you just set it to the maximum width available (assuming that the body has a fixed width) and give it a transparent background, it shouldn't matter how many sub-nav items there are.
Jacob
thank you for your answer
Baha