Hover on Menu dropdown - How to make Hover effect not to disappear? - html

I have some serious problem with my menu and its hover effect.
I have a very basic menu, which has a submenu:
<ul id="menu">
<li>Menu1</li>
<li>Menu2
<ul>
<li>SubMenu1</li>
<li>SubMenu2</li>
</ul>
</li>
<li>Menu3</li>
</ul>
Here is the CSS I'm using:
#menu li {
display: inline;
}
#menu li a {
padding: 10px;
}
#menu li a:hover {
background: #000;
}
#menu ul ul {
display: none;
}
#menu ul li:hover > ul {
display: block;
}
#menu ul ul {
width: 200px;
height: 100px;
background: #000;
}
Okay, so my problem is, that when I hover my mouse the Dropdown menu and gets my mouse on the Submenus, the Hover effect of the Parent menu item (in this case Menu2) is disappearing. So it will not have black BG when I hover the mouse on the submenu items.
Is there anything I could do to make that hover effect stay on the partent menu (Menu2)?

First problem: your selectors are wrong.
#menu IS an ul , then #menu ul ul means
an ul descendant of an ul descendant of my #menu, that is an ul
You don't have three levels of uls, so...
change ul ul to li ul.
The second problem is that you are affecting a tag on hover, but a tag is a sibling, not an ancestor (or parent) of your submenu ul.
You should then target your li, not your a.
Demo: http://jsfiddle.net/mSrkn/ (with tons of problems still there, but with the two above resolved)
#menu li {
display: inline;
}
#menu li a {
padding: 10px;
}
#menu li:hover {
background: #000;
}
#menu li ul {
display: none;
}
#menu li:hover > ul {
display: block;
}
#menu li ul {
width: 200px;
height: 100px;
background: #000;
}

The problem is with yout selectors:
#menu ul li:hover > ul {
display: block;
}
This says that any element with ID that has a child ul with lis that's hovered with a child ul should be selected. Your markup is different from this, the UL itself is the ID #menu so you have to remove the first ul from the selectors themselves:
#menu li:hover > ul {
display: block;
}
http://jsfiddle.net/V7Ltw/

You might try adding the following to your CSS
#menu li:hover{
background-color: #000;
}
By hovering over the sub-menu, you're still hovering over the parent list item.
And you should follow Kyle's answer as well as you do need to remove the first UL selector from your css.

You have to change a lot of stuff to make this work, the basic idea is to put the submenu inside your menu items :
CSS:
#menu li {
display: inline;
}
#menu li a {
padding: 10px;
}
#menu li a:hover {
background: #000;
}
#menu ul.submenu {
display: none;
float: left; // For viewing purpose
}
#menu ul.submenu { padding: 20px; }
#menu ul.submenu:hover {
display: block;
}
#menu li:hover > ul.submenu {
display: block;
}
ul.submenu:hover + a { background: #000; }
#menu ul {
width: 500px;
height: 100px;
background: #000;
}
HTML:
<ul id="menu">
<li>Menu1</li>
<li>
<ul class='submenu'>
<li>SubMenu1</li>
<li>SubMenu2</li>
</ul>
Menu2
</li>
<li>Menu3</li>
</ul>
Demo here : http://jsfiddle.net/V7Ltw/

Related

CSS and HTML nav bar menu item

I having a problem getting menu items to reappear in CSS and html. The first two line of CSS code below, "display none", hides the menu items, in which I want. However, the second code does not make the menu items appear again when the mouse is hovering over the menu. Any help on this would be greatly appreciated.
ul li ul li ul li{
display:none;
}
ul li ul li:hover {
background: #696630;
display:block; !important;
}
The item you want to reappear are the LI's nested further down, so ensure the hover is on the parent, but the element you actually style is the correct child.
ul li ul li ul li {
display: none;
}
ul li ul li:hover ul li {
display: block;
}
Please see example below. In your code, you had !important behind the semicolon which is the wrong place. I should've been directly after block.
Don't use this when it can be avoided.
ul {
padding: 0;
margin: 0;
}
li {
padding: .5em;
cursor: pointer;
position: relative;
width: 100px;
}
li:hover {
background: #696630;
}
.sub,
.subsub {
display: none;
position: absolute;
top: 1.5em;
left: 0;
}
.main li:hover ul.sub {
display: inline-block;
}
.sub li:hover ul.subsub {
display: inline-block;
}
<ul class="main">
<li>Main
<ul class="sub">
<li>Sub
<ul class="subsub">
<li>sub sub</li>
</ul>
</li>
</ul>
</li>
</ul>

Dropdown box is not working in HTML page using CSS

when I move my cursor to the main menu("for example: Menu 2"), I can see my dropdown list under
After when i move my cursor to submenu1 it is not displaying and i can't select it too
here is my sample code:
<style type="text/css">
ul#menu, ul#menu ul.sub-menu {
padding:0;
margin: 0;
}
ul#menu li, ul#menu ul.sub-menu li {
list-style-type: none;
display: inline-block;
}
/*Link Appearance*/
ul#menu li a, ul#menu li ul.sub-menu li a {
text-decoration: none;
color: #fff;
background: #666;
padding: 5px;
display:inline-block;
}
/*Make the parent of sub-menu relative*/
ul#menu li {
position: relative;
}
/*sub menu*/
ul#menu li ul.sub-menu {
display:none;
position: absolute;
top: 30px;
left: 0;
width: 100px;
}
ul#menu li:hover ul.sub-menu {
display:block;
}
</style>
<div id="menu" align="left">
<ul id="menu">
<li>
<a href='<%=request.getContextPath()%>/auth/gs.page'>Menu1</a>
</li>
<li><a>Menu2</a>
<ul class="sub-menu">
<li>
<a href='<%=request.getContextPath()%>/auth/view.page'>submenu1</a>
</li>
</ul>
</li>
</div>
Please provide me idea to resolve it
I am guessing the behaviour you're seeing is actually the submenu disappearing as you move your mouse down from the top-level nav item.
Because of the way you've set it up, there's a small gap (1-3px depending on browser and default user styles) that means it loses the hover status (and therefore hides the submenu) as you are "unhovering" the top-level nav item when you go over that gap.
If that is what you're seeing, the easiest way to fix it is to remove the top value from ul#menu li ul.sub-menu (you have it set to 30px) and add in equivalent padding to the li of the submenu to make it look right.
ul#menu li ul.sub-menu {
display:none;
position: absolute;
left: 0;
width: 100px;
}
ul#menu li ul.sub-menu li {
padding-top: 2px;
}
Here's a fiddle with that change: http://jsfiddle.net/adnrw/J44NJ/
I don't see any problems when running your code. Could you clarify what you mean by "not displaying immediately"?
ul#menu li:hover ul.sub-menu
Should probably be:
ul#menu li:hover ul.sub-menu, ul#menu li ul.sub-menu:hover

Pure CSS dropdown menu

I'm trying to fashion a 100% CSS and HTML dropdown menu like what's seen on http://phpbb.com. When you hover over the navigation links, a new div appears just below the one you hovered onto.
What I'm trying to do is make .submenu appear just below the <li> that it's nested into by using #nav li a:hover submenu {. To my knowledge this CSS selector should select the .submenu DIV when an a element is hovered over? But it doesn't work.
#nav {
list-style-type: none;
margin: -5px 0px 0px 5px;
}
#nav li {
display: inline;
}
#nav li a {
display: block;
padding: 3px;
float: left;
margin: 0px 10px 0px 10px;
text-decoration: none;
color: #fff;
font-weight: bold;
position: relative;
}
#nav li a:hover {
text-shadow: 1px 1px #333;
}
#nav li a:hover submenu {
display: block;
color: red;
}
.submenu {
position: absolute;
display: none;
}
<ul id="nav">
<li>Home
</li>
<li>
Skins
<div class="submenu">
hello :)
</div>
</li>
<li>Guides
</li>
<li>About
</li>
</ul>
Your second to last selector is looking for a "submenu" element, you should correct this to say ".submenu"
Like this:
/*#nav li a:hover submenu {*/
#nav li a:hover .submenu {
display: block;
color: red;
}
EDIT:
To get the hover to work, you also need to adjust your CSS so that the hover is applied to the list item, instead of the anchor tag:
#nav li:hover .submenu {
display: block;
color: red;
}
Are you missing a period ('.') before submenu in the selector #nav li a:hover submenu?
Try to edit this following part.
Put a . (dot) before the submenu, since its a class.
#nav li a:hover .submenu {
display: block;
color: red;
}
#nav li:hover .submenu {
display: block;
color: red;
}
You want the submenu to appear when you hover on li, not on a, simply because you do not have items with a class submenu inside the a.
Also you could consider using s for the submenus.

Why does this menu render poorly in Firefox 3.5 and IE7? How do I adapt?

Have a look at the menus on this page:
http://www.pieterdedecker.be/labs/vspwpg/?page_id=96
They look okay in Chrome 5 (above) and IE8 (below).
When I load the page into Firefox 3.5 (above) or IE7 (below) something goes wrong.
In the first case, the arrows on the right have moved to the next row. In the second case, the menu falls apart entirely.
How do I adapt the website I'm developing to this? Is it because FF3.5 and IE7 haven't implemented W3C standards entirely or simply because my CSS doesn't make sense? My HTML code has been validated XHTML 1.0 Strict as shown here.
Update - If you don't have IE7 and you're a Windows user, you can view the site through the eyes of IE7 here without actually having to install IE7: http://spoon.net/browsers
IE7 Dropdown
As Sotiris mentioned, the easiest fix for IE7 would be to give ul#menu > li > ul a fixed width. This would cause the child <li> and <a> elements to properly take 100% of their parent width.
What's currently happening in IE7 is that your dropdown menu width is being determined by the length of your longest child element on account of the white-space: nowrap property. IE7 then doesn't properly apply this to the dropdown's <ul>, which instead takes its width from the top level menu item (104 pixels in your case).
If you still want to keep the dynamic width menus, you can fix it in IE7 with a snippet of jQuery that loops through all your links on load, finds the widest one and sets the parent <ul> to that width. It should be run in your $(window).load event handler, just after you set all ul#menu > li ul to display: block:
// Nodig om de width te kunnen raadplegen
$("ul#menu > li ul").css("display", "block");
// Loop through all dropdowns and find widest child link in each
$('ul.children').each(function(){
// Find widest link in each submenu
var widest = 0;
$(this).children('li').each(function(){
if($(this).width() > widest)
widest = $(this).width();
});
// Set submenu width to widest child link
if(widest != 0)
$(this).width(widest);
});
To fix the centered items, you'll also need to remove the text-align: center from this rule:
ul#menu > li{
background: url(img/menuitem.png) left top;
display: block;
float: left;
height: 36px;
margin-right: 1px;
position: relative;
width: 104px;
}
Finally, you'll need to make sure the hasLayout flag is set properly on your dropdown links. You can do this by setting zoom: 1 on the following rule:
ul#menu > li > a, ul#menu > li > ul a {
zoom: 1;
display: block;
text-decoration: none;
white-space: nowrap;
}
Firefox 3.5 Submenu Indicator
This is an easier fix. Add the ul#menu > li > ul > li a declaration and change your span.sf-sub-indicator rule as follows:
/* Makes the link a coordinate map for span.sf-ub-indicator */
ul#menu > li > ul > li a {
position: relative;
padding-right: 10px;
}
ul#menu > li > ul > li a > span.sf-sub-indicator {
position: absolute;
top: 0;
right: 0;
}
This will absolutely position the indicator to the far right of your link. Note you'll need to apply this fix for IE7 as well otherwise your submenus will be pushed down one link too far.
change class ul#menu > li > a, ul#menu > li > ul a to -
ul#menu > li > a, ul#menu > li > ul a
{
display:inline-block;
text-decoration:none;
white-space:nowrap;
width:95%;
}
and move <span>>></span> out of the anchor tag. this works on firefox could not try it on IE
Update
<style type="text/css">
.menucontrol a:link, .menucontrol a:active, .menucontrol a:visited
{
border: 1px solid orange;
color: white;
background-color: orange;
}
.menucontrol a:hover
{
background-color: #fff;
color: #333;
}
.menucontrol, .menucontrol ul
{
margin: 0;
padding: 0;
list-style-type: none;
list-style-position: outside;
position: relative;
line-height: 1.5em;
}
.menucontrol a:link, .menucontrol a:active, .menucontrol a:visited
{
display: block;
padding: 0px 5px;
text-decoration: none;
}
.menucontrol li
{
float: left;
position: relative;
}
.menucontrol ul
{
position: absolute;
width: 12em;
top: 1.5em;
display: none;
}
.menucontrol li ul a
{
width: 12em;
float: left;
}
.menucontrol ul ul
{
top: auto;
}
.menucontrol li ul ul
{
left: 12em;
margin: 0px 0 0 10px;
}
.menucontrol li:hover ul ul, .menucontrol li:hover ul ul ul, .menucontrol li:hover ul ul ul ul
{
display: none;
}
.menucontrol li:hover ul, .menucontrol li li:hover ul, .menucontrol li li li:hover ul, .menucontrol li li li li:hover ul
{
display: block;
}
</style>
<body style="font-family: Consolas; font-size: 11px;">
<ul class="menucontrol">
<li>1 HTML</li>
<li>2 CSS</li>
<li>3 Javascript
<ul>
<li>3.1 jQuery
<ul>
<li>3.1.1 Download<ul>
<li>3.1.1 Download<ul>
<li>3.1.1 Download</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.2 Mootools</li>
<li>3.3 Prototype</li>
</ul>
</li>
<script type="text/javascript">
function mainmenu()
{
$(" .menucontrol ul ").css({ display: "none" }); // Opera Fix
$(" .menucontrol li").hover(function()
{
$(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(400);
}, function()
{
$(this).find('ul:first').css({ visibility: "hidden" });
});
}
$(document).ready(function()
{
mainmenu();
});

CSS selector not working?

Hey guys, thanks in advance for any help or input. I am having trouble understanding CSS selectors and i have read the docs..
I am trying to refer to the UL with the id dropdown in my stylesheet. I was under the assumption that i was able to refer to any elements like:
#dropdown ul
{
}
This method however does not seem to work :s.. Am i misunderstanding CSS selectors? The elements in my actual code are nested deeper than this structure but i presume the principle is the same?
<div id="wrapper">
<ul id="dropdown">
<li class="sub">Dropdown
<!-- Sub Menu -->
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<!-- End Submenu -->
</li>
</ul>
</div>
/* Dropdown Menu */
#dropdown ul
{
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
#dropdown ul li
{
display: block;
position: relative;
float: left;
}
#dropdown li ul
{
display: none;
}
#dropdown ul li a
{
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #2C5463;
margin-left: 1px;
white-space: nowrap;
}
#dropdown ul li a:hover
{
background: #617F8A;
}
#dropdown li:hover ul
{
display: block;
position: absolute;
}
#dropdown li:hover li
{
float: none;
font-size: 11px;
}
#dropdown li:hover a
{
background: #617F8A;
}
#dropdown li:hover li a:hover
{
background: #95A9B1;
}
Try
ul#dropdown
{
}
This will select the ul element with ID dropdown.
With
#dropdown ul
you are trying to locate a ul element within an element with id dropdown. This will select all ul elements inside the #dropdown ul, however many levels deep.
edit: it's right as mario wrote.
edit2: im sorry for being 5seconds too slow, i just wanted to help. For completition of my post:
ul#dropdown or #dropdown is the right selection
#dropdown ul means "select any ul that is a direct or indirect child of #dropdown". That is not what you want, I think. Try #dropdown alone (you don't need to mention ul as IDs are exclusive, meaning you should only have one #dropdown in a page).