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;
}
Is there any way to add the scroll bar for it? A: You can try to add the scroll bar for submenu manually. For this purpose you should open your page where you added the css3menu in any text editor and add class="scroll" into the <ul></ul> tag.
Add data-bs-spy="scroll" to the element that should be used as the scrollable area (often this is the <body> element). Then add the data-bs-target attribute with a value of the id or the class name of the navigation bar ( . navbar ). This is to make sure that the navbar is connected with the scrollable area.
To easily add scrollspy behavior to your topbar navigation, add data-bs-spy="scroll" to the element you want to spy on (most typically this would be the <body> ). Then add the data-bs-target attribute with the ID or class of the parent element of any Bootstrap .
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:
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.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:
-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 thereul
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).
If you have predictable list item heights, this is fairly trivial. Consider the following example:
<ul class="sub-menu">
<li>This is option one.</li>
<li>This is a second option.</li>
<li>We'll need three.</li>
<li>And now these are out of view.</li>
<li>I'm the last option.</li>
</ul>
Here we have this list of five items. I will instruct each of the list items themselves to have a font-size
and line-height
of 1em
, with a padding of .25em
on the top and bottom:
.sub-menu li {
font-size: 1em;
padding: .25em 1em;
line-height: 1em;
}
So now we know that each list item is 1.5em
tall. As such, we can now set the max-height
on our parent element so that it only shows three list items at a time:
.sub-menu {
padding: 0;
max-height: 4.5em; /* 1.5 x 3 */
overflow-y: auto;
}
The results:
Online Demo: http://jsfiddle.net/euSWB/16/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With