3rd level. Centered menu.
I have a centered, 3rd level that is displayed on hover of the 2nd level. What am I missing to keep it hidden until it hovers over it directly?
PS - I know the code would be cleaner s> in it as I've seen on the internet, but not sure how to clean it up, so I hope the code is clear as it is.
Thank!
JS Fiddle: DEMO
#centeredmenu {
clear:both;
float:left;
margin:0 0 30px 0;
padding:0;
border-bottom:1px solid #000; /* black line below menu */
width:100%;
font-family:Arial, Helvetica, sans-serif; /* Menu font */
z-index:1000; /* This makes the dropdown menus appear above the page content below */
position:relative;
background-color: #000;
}
/* Top menu items */
#centeredmenu ul {
margin:0;
padding:0;
list-style:none;
float:right;
position:relative;
right:50%;
}
#centeredmenu ul li {
margin:0 0 0 1px;
padding:0;
float:left;
position:relative;
left:50%;
top:1px;
}
#centeredmenu ul li a {
display: block;
margin: 0;
padding: 10px 20px;
font-size: 1em;
line-height: 1em;
text-decoration: none;
color: #fff;
font-weight: bold;
border-bottom: 1px solid #000;
}
#centeredmenu ul li a:hover {
background: #a3c2df; /* Top menu items background color */
color: #fff;
border-bottom: 1px solid #03f;
}
#centeredmenu ul li:hover a {
background: #a3c2df; /* Top menu items background color */
color: #000;
border-bottom: 1px solid #03f;
}
/* 2nd Level Items */
#centeredmenu ul ul {
display:none; /* Submenus are hidden by default */
position:absolute;
left:0;
right:auto; /* Resets the right:50% on the parent ul */
width:12em; /* Width of the drop-down menus */
}
#centeredmenu ul ul li {
left:auto; /*Resets the left:50% on the parent li */
margin:0; /* Resets the 1px margin from the top menu */
clear:left;
width:100%;
}
/* 3rd Level Items */
#centeredmenu ul ul ul {
display:none; /* Submenus are hidden by default */
position:absolute;
top:0;
left:155px;
right:auto; /* Resets the right:50% on the parent ul */
width:12em; /* Width of the drop-down menus */
}
#centeredmenu ul ul ul li {
left:auto; /* Resets the left:50% on the parent li */
margin:0; /* Resets the 1px margin from the top menu */
clear:left;
width:100%;
}
#centeredmenu ul ul li a,
#centeredmenu ul li.active li a,
#centeredmenu ul li:hover ul li a {
font-size:0.9em;
font-weight:normal; /* Resets the bold set for the top level menu items */
background:#eee;
color:#444;
line-height:1.4em; /* Overwrite line-height value from top menu */
border-bottom:1px solid #ddd; /* Submenu item horizontal lines */
}
#centeredmenu ul ul li a:hover,
#centeredmenu ul li.active ul li a:hover,
#centeredmenu ul li:hover ul li a:hover {
background: #a3c2df; /* Submenu items background color */
color:#000; /* Submenu items hover color */
}
/* Flip the last 2nd menu so it stays within the page */
#centeredmenu ul ul.last {
left:auto; /* Resets left:0; value */
right:0; /* Set right value instead */
}
/* Make the 2nd menus appear on hover */
#centeredmenu ul li:hover ul {
display:block; /* Show the submenus */
}
/* Make the 3rd menus appear on hover */
#centeredmenu ul li:hover ul ul{
display:block; /* Show the submenus */
}
<div id="centeredmenu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Documents</a>
<ul>
<li><a href="#">Reading</a></li>
<li><a href="#">Writing</a>
<ul>
<li><a href="#">Excerpt 1</a></li>
<li><a href="#">Excerpt 2</a></li>
<li><a href="#">Excerpt 3</a></li>
<li><a href="#">Excerpt 4</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
source to share
#centeredmenu ul li:hover ul
in your CSS matches both menu levels. Elements li:hover ul
force the browser to look for a tag ul
with a li:hover
as its parent. It doesn't have to be a direct parent, it can also be a grandfather, grandgrandparent, etc. Try to see if you can see why this is the case for both menu levels. http://learn.shayhowe.com/html-css/getting-to-know-css/ may give you some insight into how CSS selectors work
A quick fix would be to change #centeredmenu ul li:hover ul
on #centeredmenu ul li:hover > ul
and remove #centeredmenu ul li:hover ul ul
.
I played around with something similar today, see http://codepen.io/ckuijjer/pen/huyxn for my example. I tried to use mostly classes and hardly styled the element.
source to share
#centeredmenu ul li:hover ul
means all ul
elements under the hovering element (regardless of depth)! This, in turn, means that when you hover over li
, each child of ul
that element li
will have associated rules. #centeredmenu ul li:hover > ul
means the first child (- only the first descendants)! This prevents the hang from propagating below the intended level. "#centeredmenu ul li: hover ul ul" has the same problem, except that propagation level 2 starts and continues to the end.
#centeredmenu {
clear: both;
float: left;
margin: 0 0 30px 0;
padding: 0;
border-bottom: 1px solid #000;
/* black line below menu */
width: 100%;
font-family: Arial, Helvetica, sans-serif;
/* Menu font */
z-index: 1000;
/* This makes the dropdown menus appear above the page content below */
position: relative;
background-color: #000;
}
/* Top menu items */
#centeredmenu ul {
margin: 0;
padding: 0;
list-style: none;
float: right;
position: relative;
right: 50%;
}
#centeredmenu ul li {
margin: 0 0 0 1px;
padding: 0;
float: left;
position: relative;
left: 50%;
top: 1px;
}
#centeredmenu ul li a {
display: block;
margin: 0;
padding: 10px 20px;
font-size: 1em;
line-height: 1em;
/* [disabled]background: #ddd; */
text-decoration: none;
color: #fff;
font-weight: bold;
border-bottom: 1px solid #000;
}
#centeredmenu ul li a:hover {
background: #a3c2df;
/* Top menu items background colour */
color: #fff;
border-bottom: 1px solid #03f;
}
#centeredmenu ul li:hover a {
background: #a3c2df;
/* Top menu items background colour */
color: #000;
border-bottom: 1px solid #03f;
}
/* 2nd Level Items */
#centeredmenu ul ul {
display: none;
/* Sub menus are hiden by default */
position: absolute;
left: 0;
right: auto;
/*resets the right:50% on the parent ul */
width: 12em;
/* width of the drop-down menus */
}
#centeredmenu ul ul li {
left: auto;
/*resets the left:50% on the parent li */
margin: 0;
/* Reset the 1px margin from the top menu */
clear: left;
width: 100%;
}
/* 3rd Level Items */
#centeredmenu ul ul ul {
display: none;
/* Sub menus are hiden by default */
position: absolute;
top: 0;
left: 155px;
right: auto;
/*resets the right:50% on the parent ul */
width: 12em;
/* width of the drop-down menus */
}
#centeredmenu ul ul ul li {
left: auto;
/*resets the left:50% on the parent li */
margin: 0;
/* Reset the 1px margin from the top menu */
clear: left;
width: 100%;
}
#centeredmenu ul ul li a,
#centeredmenu ul li.active li a,
#centeredmenu ul li:hover ul li a {
font-size: 0.9em;
font-weight: normal;
/* resets the bold set for the top level menu items */
background: #eee;
color: #444;
line-height: 1.4em;
/* overwrite line-height value from top menu */
border-bottom: 1px solid #ddd;
/* sub menu item horizontal lines */
}
#centeredmenu ul ul li a:hover,
#centeredmenu ul li.active ul li a:hover,
#centeredmenu ul li:hover ul li a:hover {
background: #a3c2df;
/* Sub menu items background colour */
color: #000;
/* Sub menu items hover colour */
}
/* Flip the last 2nd menu so it stays within the page */
#centeredmenu ul ul.last {
left: auto;
/* reset left:0; value */
right: 0;
/* Set right value instead */
}
/* Make the 2nd menus appear on hover */
#centeredmenu ul li:hover > ul {
display: block;
/* Show the sub menus */
}
#centeredmenu ul li ul li ul {
display: none;
}
#centeredmenu ul li ul li:hover ul {
display: block;
}
<div id="centeredmenu">
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">Documents</a>
<ul>
<li><a href="#">Reading</a>
</li>
<li><a href="#">Writing</a>
<ul>
<li><a href="#">Excerpt 1</a>
</li>
<li><a href="#">Excerpt 2</a>
</li>
<li><a href="#">Excerpt 3</a>
</li>
<li><a href="#">Excerpt 4</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
source to share