Adding scroll bar to dynamic ul li

css, html, javascript

Solution

I've made a couple of adjustments to your original stylesheet, and now it will show the scroll bar, but only when it exceeds the height of 3 list items (63 pixels in this case). Here's the final CSS code:

#mnav {
    margin-left: -30px;
    margin-right: -30px;
}
#mnav li {
    float: left;
    list-style: none;
    margin:0 10px;/*Keeping 10px space between each nav element*/
}
#mnav li a,/*These can all be merged into a single style*/
#mnav li a:link,
#mnav li a:visited,
#mnav li a:hover,
#mnav li a:active {
    text-decoration: none;
}
#mnav li ul {
    display: none;/*This is the default state.*/
    z-index: 9999;
    position: absolute;
    width: 400px;
    max-height:63px;/*The important part*/
    overflow-y:auto;/*Also...*/
    overflow-x:hidden;/*And the end of the important part*/
    margin: 0px;
    padding-left:5px;/*Removing the large whitespace to the left of list items*/
    border: 1px solid #ddd;
}
#mnav li:hover ul, #mnav li.sfhover ul {
    display:block;/*This is the hovered state*/
}
#mnav li ul li a, #mnav li ul li a:link, #mnav li ul li a:visited {
    display: block;
    margin: 0;
    text-decoration: none;
    z-index: 9999;
    border-bottom: 1px dotted #ccc;
    width:400px;
}
#mnav li ul li a:hover, #mnav li ul li a:active {
    display: block;
    margin: 0;
    text-decoration: none;
}

Here's the demo of it. I've also, for demonstration purposes, inserted 2 extra `<li>` elements to the Home and SQL Server vs Oracle items. The Home item will show how the popup behaves if there are fewer than 3 list items there.

To explain each of the changed bits, first the code that actually does the trick:

- defining the `max-height` to 63 will make the ul behave normally if it stays under 63px high, but if it exceeds that, it will overflow and show a scroll bar. If you want to show more items, just increase the `max-height` to the desired height. Currently the height of each item is 21px, so I used that to get the 63px for 3 items.

- Since the overflow should only show a scroll bar for the vertical direction, only `overflow-y:auto;` should be there, and `overflow-x:hidden` to prevent a scrollbar in the horizontal direction.

And then the other general changes I made:

- I've added 20px margin between items (10px on either side of the element) to make the list look a bit better here. You may want to apply your own style though, this is just for this demo.

- I've changed your hiding technique from 'shoving it off to `-999em` to the left' to hiding it via `display:none`. This is better to work with, since elements that are `display:none` will not render in search engines, so this will help in those situations. In general I think hiding things with `display:none` is just better than shoving it off the screen, while it's still actually there

- I've removed the padding to the left of the `ul` since it just looked quite bad. No need for the default padding if you're not using it for a dotted/numbered list.

This should also work considering your comment to Zachary Carter's answer, since this won't show a huge white box if you define the `max-height` to 210px (10 items).

Problem

I searched through many posts and forum as i thought this might be a basic stuff but didnot find it so asking here,All i am trying to do is add scroll bar if the height is more than certain limit lets say if menu items are more than 3. I have created a jsfiddle http://jsfiddle.net/euSWB/ If you are not able to access it then here is the HTML and CSS HTML ``` <ul id="mnav"> <li><a><b>Home</b></a> </li> <li><a><b>SQL Server vs Oracle</b></a> <ul> <li><a>Basic SQL : Declare variables and assign values</a></li> <li><a>Basic SQL : Inner Join, Outer Join and Cross Join</a></li> <li><a>Basic SQL : Padding and Trimming</a></li> <li><a>Basic SQL : Union,Except/Minus,Intersect</a></li> <li><a style="border-bottom-color: currentColor; border-bottom-width: 0px; border-bottom-style: none;">Update from Select</a></li> </ul> </li> <li><a href="#"><b>SSIS</b></a> <ul> <li><a>Coalesce in SSIS</a></li> <li><a >Universal CSV Generator</a></li> <li><a >Parsing a row into multiple in CSV</a></li> <li><a style="border-bottom-color: currentColor; border-bottom-width: 0px; border-bottom-style: none;" >XML Task in SSIS</a></li> </ul> </li></ul> ``` CSS ``` #mnav { margin-left: -30px; margin-right: -30px; } #mnav li { float: left; list-style: none; } #mnav li a, #mnav li a:link, #mnav li a:visited { text-decoration: none; } #mnav li a:hover, #mnav li a:active { text-decoration: none; } #mnav li:hover, #mnav li.sfhover { position: static; } #mnav li ul { display: block; z-index: 9999; position: absolute; left: -999em; width: 400px; margin: 0px; border: 1px solid #ddd; } #mnav li:hover ul, #mnav li.sfhover ul { left: auto; } #mnav li ul li a, #mnav li ul li a:link, #mnav li ul li a:visited { display: block; margin: 0; text-decoration: none; z-index: 9999; border-bottom: 1px dotted #ccc; width: 500px; } #mnav li ul li a:hover, #mnav li ul li a:active { display: block; margin: 0; text-decoration: none; } ```

Original source