How to dynamically append li to ul?

CSS:  How do I solve this UL problem - please help?

  • It is a UL LI UL This is how I would like it: The top UL assumes the width of the sub UL. How can I get it to just go to the left as per the second image? Thanks

  • Answer:

    I think you can position the nested UL absolutely. ul {position:relative;} li {position:relative;float:left;} li ul {position:absolute;top:20px;}

Jared Alessandroni at Quora Visit the source

Was this solution helpful to you?

Other answers

This should work: http://jsfiddle.net/csswizardry/WZE9H/ Here we abstract the nav out into three separate things; we have your generic `.nav` object, then we have a `.site-nav` extension (I assumed this would be your main site nav, alter as necessary) and then finally we deal with your dropdown stuff. Here is an example of why abstracting each out means we can do a lot more with the three components: http://jsfiddle.net/csswizardry/WZE9H/1/ This was a quick ten minute hack-together so please tidy stuff up as necessary :) Harry

Harry Roberts

Is the width of the <li> fixed? If so, I think that you might me able to make make overflow:visible. The second <ul> should then be shown without displacing the other <li>, as long as the overflow is not covered by any other elements.   It would help if I could see the source so that I can see what you're doing already, inbox me and you could email it to me and I might see what I can do?   Otherwise, look at some examples of dropdown menus, you should see a solution there.

Thomas Foster

non-programmatically you can just assign the separately assigned ul elements an id to target so that you can align them accordingly. [code] #navbar {    position: absolute;    width: 100%;    background-color: pink;    top: 0;    left: 0;    margin: 0;    padding: 0; }    #navbar li {    list-style: none;    float: left; }    #navbar li a {    display: block;    padding: 3px 8px;    text-transform: uppercase;    text-decoration: none; }    #navbar li ul {    display: none;  } <!-- // these are relative, need to tweak based on your context --> #navbar li ul#about { margin-left: 65px; } #navbar li ul#classroom { margin-left: 163px; }    #navbar li:hover ul, #navbar li.hover ul {    position: absolute;    display: inline;    left: 0;    width: 100%;    margin: 0;    padding: 0; }    #navbar li:hover li, #navbar li.hover li {    float: left; } ////////////////////////////////////////////////////////////////////////////// <body>    <div id="wrap">       <ul id="navbar">          <li><a href="#">Home</a>             <ul>                <li><a href="#">Current Exhibit</a></li>                <li><a href="#">Coming Exhibit</a></li>                <li><a href="#">Events</a></li>             </ul>          </li>          <li><a href="#">About Us</a>             <ul id="about">                <li><a href="#">ABOUT OUR SCHOOL</a></li>                <li><a href="#">OUR TEACHERS</a></li>                <li><a href="#">STUDENT SUPPORT TEAM</a></li>             </ul>                  </li>          <li><a href="#">VIRTUAL CLASSROOM</a>             <ul id="classroom">                <li><a href="#">something here</a></li>                <li><a href="#">something here</a></li>             </ul>                  </li>       </ul>             <br />       <br /> <!--       // rest of page -->       <p>Ut fermentum ipsum sit amet libero. Duis fringilla libero sit amet nisl. Cras tincidunt tortor. Praesent consectetuer orci vel ipsum. Nullam tincidunt, lectus non iaculis malesuada, erat mi pulvinar enim, id laoreet tellus est sed felis. Etiam pretium, sem non fringilla tempus, arcu nulla eleifend eros, sed blandit mauris lacus et purus. Aenean et quam. Proin in ante. Nullam sodales lacinia dolor. Duis pharetra cursus tellus. Proin quam massa, imperdiet at, fermentum in, auctor vitae, eros.</p>    </div> </body> [/code]

Vince Baskerville

I like using the superfish menu which uses CSS approximately like Jared describes (and adds support for an otherwise pure CSS solution for old IE browsers), so I'd go with Jared's answer.  I'm not a fan of separating each nav item into its own UL as described by Damien because I see the entire nav as a single, semantic, thing and therefore a single UL with nested ULs.

Tim Arnold

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.