HTML/CSS: show submenu next to parent MENU, not parent item - html

I have a three-level navigation, the third level (sub-submenu) displays when hover on the second one (submenu).
The sub-submenu appears relative to the parent item. But I want it to appear simply next to the submenu, so always on the same place, regardless which submenu-item gets hovered. So the first item of the sub-submenu (e.g. "Second-One") is always on the same height as the first item of the submenu ("First")
Here is a JSFiddle for my actual status: http://jsfiddle.net/fc0rwbqu/
Thanks in advance!
Code:
<ul id="nav">
<li>Home</li>
<li>Numbers
<ul>
<li>First
<ul>
<li>First-One</li>
<li>First-Two</li>
<li>First-Three</li>
</ul>
</li>
<li>Second
<ul>
<li>Second-One</li>
<li>Second-Two</li>
<li>Second-Three</li>
</ul>
</li>
<li>Third
<ul>
<li>Third-One</li>
<li>Third-Two</li>
<li>Third-Three</li>
</ul>
</li>
</ul>
</li>
CSS:
#nav {
position: relative}
#nav li a,#nav li { float:left;
margin-left: 20px;
background-color: #ddd;}
#nav li {
list-style:none;
position:relative;}
#nav li ul {
display:none;
position:absolute;
left:0; width: 100px;
top:90%;
padding:0;
margin:0;}
#nav li:hover > ul {
display:block;}
#nav li ul li {
float:none;
height: 35px;
min-width: 100px;
line-height: 35px;
border-right: 0;
display:block;}
#nav li ul li ul {
display:none; margin: 0 0 0 10px;
width: 100px; z-index:9999;
float: left !important; overflow: display;
background-color: #f06;}
#nav li ul li:hover ul {
left: 100px;
top:0;}

Something like this?? http://jsfiddle.net/fc0rwbqu/1/
Appled position: relative to ul tag.

Related

Css display submenu on menu item hover

I have a menu where each menu item has a background image, when hovering over a menu item it is underlined by scrolling up the corresponding background image. Now I also want to display the submenu(with text items, no background image), but the submenu text isn't diplayed , if I set the border of the submenu I can see a rectangle with no proper content. I don't see what's wrong with the code, please help me correct (and improve) it.
//html structure
<div id="menu_top">
<ul id="menu">
<li class="home">Home</li>
<li class="menu">Menu
<ul class="submenu">
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
</li>
<li class="contact">Contacts</li>
<li class="about">About</li>
</ul>
</div>
//css styles
#menu_top
{
margin: 0 auto;
text-align: center;
background: url(../images/nav-bg.jpg) no-repeat 0 0;
height: 150px;
width: 815px;
}
ul#menu
{
list-style: none;
margin: 0;
position: relative;
}
ul#menu li
{
background-position: left top;
background-repeat: no-repeat;
display: block;
text-indent: -9000px;
position: absolute;
outline: none;
}
/*Menu items*/
// all menu items similar to this ...
ul#menu li.menu
{
background-image: url(../images/nav-menu.png);
height: 50px;
width: 116px;
top: 30px;
left: 380px;
}
/* underline menu items*/
ul#menu li:hover
{
background-position: left bottom;
}
#menu >li ul
{
position:absolute;
left:0px;
top:50px;
width:100%;
}
#menu >li ul>li
{
list-style:none;
}
#menu >li:hover > ul
{
display:block;
}
The sub menu items are not being displayed because you have set a text-indent:-9000px for ul#menu li, which means the text indent will apply to all the li items under the ul #menu. To display the sub menu items apply the below styles to the submenu li
#menu >li ul>li
{
list-style:none;
text-indent:0;
position:relative; // this is set to remove position absolute set for ul#menu li
}
OR
#menu .submenu li{
position:relative;
text-indent:0;
}

css styling of drop down menu

I am trying to style my drop down menu so it would look like on my picture:
![drop down menu]()
So far I got this:
`<nav>
<ul id="home">
<li>HOME</li>
</ul>
<ul id="about">
<li>ABOUT</li>
</ul>
<ul id="business">
<li>BUSINESS GROWTH
<ul class="sub-menu">
<li>BUSINESS GROWTH </li>
<li>FAQs</li>
<li>FEES & SCHEDULING</li>
<li>QUESTIONNAIRE</li>
</ul>
</li>
</ul>
`
#business,
#home,
#about
{
list-style-type: none;
}
#business li{
position:relative;
background-color: #004473;
display:inline-block;
width: 180px;
}
#business li a {
color: #fff;
text-decoration: none;
display:inline-block;
}
#business li ul {
position: absolute;
left: -9999px;
background-color: grey;
margin:0;
padding:0;
list-style-type: none;
width: auto;
}
#business li:hover ul {
left: 0px;
display:inline-block;
}
#business li ul li {
background-color: #004473;
width: 180px;
display:inline-block;
margin:0;
padding:0;
}
#business li ul li a {
color:#fff;
text-decoration: none;
font-style: italic;
}
#business li ul li a:hover {
text-decoration:underline;
}
main issues:
display menu horizontally (home about business growth should be on the same horizontal line)
arrow shape of main menu item
a gap between main menu items and the sub-items
http://codepen.io/anon/pen/oDzbH
Could you please help? Very appreciated.
display menu horizontally (home about business growth should be on the same horizontal line)
add this in css
nav,nav ul{
display:inline-block;
}
arrow shape of main menu item
i assume you want it on hover
ul li > a:hover{
display:block;
background:#fff url(images/arrow.png) no-repeat center bottom;
}
a gap between main menu items and the sub-items
add this in css
ul.sub-menu{
margin-top:10px; /* or whatever suits your layout*/
}

Aligning drop-down menu in CSS

I can't seem to make the drop-down <ul> align with it's parent <li>.
What am I doing wrong?
HTML:
<nav id="toolbar">
<ul>
<li>
Section1</li>
<li>
Section2
<ul>
<li>
SubA
</li>
</ul>
</li>
<li>
Section3
<ul>
<li>SubB</li>
</ul>
</li>
<li>
Section4
</li>
</ul>
</nav>
CSS:
#toolbar ul {list-style-type:none;
}
#toolbar ul li {display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {display:none;
}
#toolbar ul li:hover ul {display: block;
position:absolute;
}
Fiddle
ul elements have a default padding-left value applied by the user-agent, so by removing that you get the alignment you asked for (There is also a margin added by you, but I'm gonna ignored it since you've added it yourself).
#toolbar ul {
padding-left: 0;
}
or (if you want to target only the sub-menu):
#toolbar ul ul {
padding-left: 0;
}
Demo
What adonis said was absolutely correct. There are certain other elements which take default padding and margin. So try using reset before styling any element.
*{margin:0;padding:0;}
if no list item of your page wants default bullets you may also include list-style:none in the above reset lines of code.
There is margin and padding in this case (you add the margin yourself) which is affecting it.
View here for a fix
(this has outlines for more visual proof)
CSS:
#toolbar ul {
padding: 0;
list-style-type:none;
}
#toolbar ul > li {
display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {
display:none;
}
#toolbar ul li:hover ul {
display: block;
position:absolute;
}
#toolbar ul ul > li {
margin: 0;
}
Try this one i have pasted the code here
HTML:
<nav id="toolbar">
<ul>
<li>
Section1</li>
<li>
Section2
<ul>
<li>
SubA
</li>
</ul>
</li>
<li>
Section3
<ul>
<li>SubB</li>
</ul>
</li>
<li>
Section4
</li>
</ul>
</nav>
CSS:
#toolbar ul {list-style-type:none;
}
#toolbar ul li {display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {display:none;
}
#toolbar ul li:hover ul {
display: block;
position:absolute;
}
#toolbar ul li:hover ul li{
margin-left: -30px;
}

CSS horizontal menu with sub options

I'm trying to add sub menu under main menu items but failed to do so. I've added display:none to hide sub menu, display:block to show when mouse is over main menu etc to some of the tags in CSS but none of them worked. Perhaps I added to wrong places.
I've cleared all my faulty codes not to deal with messy code instead giving you clear one to modify it.
Sub menu shouldn't be visible unless mouse is over its parent menu. Also sub menu should appear right under its parent menu.
Thanks in advance
CSS
<style>
.menu{
width: 100%;
background-color: #666666; }
.menu ul{
margin: 0; padding: 0;
float: left;}
.menu ul li{
display: inline;
position: relative;}
.menu ul li a{
float: left; text-decoration: none;
color: white;
padding: 10.5px 11px;
background-color: #333; }
.menu ul li a:hover, .menu ul li .current{
color: #fff;
background-color:#0b75b2;}
</style>
HTML
<div class="menu">
<ul>
<li>HOME
<ul>
<li>Home</li>
<li>Home short</li>
<li>Home very long</li>
</ul>
</li>
<li>ADMINISTRATOR
<ul>
<li>Admin</li>
<li>Admin short</li>
</ul>
</li>
<li>STAFF
<ul>
<li>staff</li>
</ul>
</li>
<li>LOGOUT</li>
</ul>
<br style="clear:left"/>
</div>
Change to .menu ul li ul{
position: absolute;}
If I understand your problem correctly, works fine now.
so..
.menu ul li ul{
position:absolute;
margin-top:40px;
width:150px;}
.menu ul li ul li{
display:block;}
To hide until hover, .menu ul li ul{ display:none; } .menu ul li:hover ul{display:block; }

drop down menu 3 level (horizontal followed by 2 verticals)

I have a menu bar. The menu bar is horizontal. The sub menu is then extended vertically upon hovering. The items in this sub menu includes Manage subjects, Manual Crawl, Crawl Interval and Archive List. When the mouse is hover to Manage subjects, it should prompt another dropdown list at the right side of it to product a sub sub menu. However, I can make this sub sub menu to appear on the right. its over lapping my sub menu. as i am really new to CSS, i definitely need help in this. i have a feeling i am not even editing
#menu ul li ul li ul, #menu ul li ul li:hover ul ,#menu ul li ul li:hover ul li and #menu ul li ul li ul li a:hover. thank you.
HTML
<div id="menu">
<ul>
<li>Home</li>
<li>Executive Summary</li>
<li><a href="#" > Visual Analytics</a></li>
<li><a href="#" >Settings</a>
<ul>
<li><a href="#" >Manage Subject</a></li>
<ul>
<li>Add Subject</li>
<li>Edit Subject</li>
<li>Delete Subject</li>
<li>Export Subject</li>
</ul>
<li>Manual Crawl</li>
<li>Crawl Interval</li>
<li>Archive List</li>
</ul>
</li>
</ul>
</div>
CSS
#menu {
position: relative;
float: left;
width: 1200px;
height: 35px;
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
padding: 0;
background-color:#000;
text-align: center;
z-index:1;
}
#menu ul {
position: relative;
list-style: none;
display: inline-block;
margin: 0;
padding: 0;
}
#menu ul li {
position: relative;
float: left;
height: 100%;
padding: 0;
line-height: 35px;
}
#menu ul li a {
position: relative;
height: 100%;
width: auto;
float: left;
text-decoration: none;
padding: 0px 10px 0px 10px;
margin: 0 3px;
color: #fff;
text-align: center;
}
#menu ul li ul {
display: none;
width: 150px; /* Width to help Opera out */
background-color: rgba(0,0,0,0.5);
}
#menu ul li:hover ul {
display:block;
position: absolute;
top: 35px;
left: 0;
margin: 0;
padding: 0;
z-index: 1;
width:150px;
}
#menu li li a{
height: 35px;
width: 150px;
float: left;
text-decoration: none;
padding: 0px;
margin: 0 0px;
color: #fff;
text-align: center;
}
#menu ul li ul li a:hover{
background: rgba(255,255,255,0.5);
width:150px;
float:left;
}
#menu ul li ul li ul{ display:none; position:absolute;background-color:rgba(28,28,240,0.5);}
#menu ul li ul li:hover ul { display:block; position:absolute; top:0px;background-color:#fff;}
#menu ul li ul li:hover ul li { list-style:none; float:none; margin-left:1px; padding:0px; position:relative;background-color:#fff;}
#menu ul li ul li ul li a:hover{
background-color: rgba(28,28,240,0.5);
width:150px;
float:left;
}​
First Question
You have to wrap the submenus ul in the higher level menuitem li to make them appear correctly. Here is an easy example for the html structure.
<ul id="menu">
<li><a>mainmenu</a></li>
<li><a>mainmenu</a></li>
<li><a>mainmenu</a>
<ul>
<li><a>first level submenu</a></li>
<li><a>first level submenu</a></li>
<li><a>first level submenu</a>
<ul>
<li><a>second level submenu</a></li>
<li><a>second level submenu</a></li>
<li><a>second level submenu</a></li>
<li><a>second level submenu</a></li>
</ul>
</li>
<li><a>first level submenu</a></li>
<li><a>first level submenu</a></li>
</ul>
</li>
<li><a>mainmenu</a></li>
<li><a>mainmenu</a></li>
</ul>
Second Question
The trick for moving the second level submenus to the right was giving them the property left:150px.
#menu ul ul{
left:150px;
}
Improvements of your Stylecheet
By the way notice that you can specify all properties of a initially hidden box in css first. Then your :hover selector only needs to set the display:block rule to show the element. If it is hidden by display:none the already set background color won't be visible.
But there are other points you could improve your stylecheet, too. For example you can set properties for all submenus with the selector #menu ul (if your ul of the menu has the id menu) because they are all lists in your menu. If you want to only set something for the first level submenu you can use #menu > li > ul as it only approaches direct children. For the second level submenus use #menu ul ul.
Using these techniques your stylecheet is more abstract. My solution also works for and unlimited number of levels of submenus. You could for example implement a third level submenu only by adding the html code. The stylecheet can handle it.
Example
Here is a working fiddle of your example: http://jsfiddle.net/m8Bcb/4/
There you can see the improved source code (both html and css) and a working live demo. And with this edit the stylecheet is also commented for your understanding. I hope that helps you and you will continue learning css!
Your submenu needs to be inside of the list item of the parent menu.
Modify your HTML like so:
<ul>
<li>Home</li>
<li>Executive Summary</li>
<li><a href="#" > Visual Analytics</a></li>
<li><a href="#" >Settings</a>
<ul>
<li><a href="#" >Manage Subject</a>
<ul>
<li>Add Subject</li>
<li>Edit Subject</li>
<li>Delete Subject</li>
<li>Export Subject</li>
</ul>
</li>
<li>Manual Crawl</li>
<li>Crawl Interval</li>
<li>Archive List</li>
</ul>
</li>
</ul>
Notice that all I did was move the </li> to be after the </ul> of the submenu.
change your css like this
#menu ul li:hover > ul {
display:block;
position: absolute;
top: 35px;
left: 0;
margin: 0;
padding: 0;
z-index: 1;
width:150px;
}
when you hover li the whole ul shows, thats why you must select direct siblings.
and add
#menu ul li ul li ul {
display: none;
}
Just to add to danijar's js fiddle solution.
You may wish for all sub menus to display relative to the first list ('main menu items') however a better solution may be to display lists or 'sub menus' relative to their parent list item.
/* ensure each submenu displays relative to it's parent list item */
#menu ul li {
position: relative;
}
Here is an example of a menu that you may want to add the above line of styling for:
http://jsfiddle.net/codk1sgm/5/