Search code examples
htmlcssdrop-down-menunavigationcenter

3rd Level Centered Menu Dropdown Navigation Issue


I have a Centered, 3rd Level Navigation that is showing upon hover of the 2nd Level. What am I missing to keep it hidden until hovering over it directly?

P.S. - I know the code would be cleaner with > in it as I've seen online, but not sure how to clean this up so I hope the code is clear as is.

Thank you!

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>

Solution

  • #centeredmenu ul li:hover ul in your CSS matches both levels of the menu. The li:hover ul parts makes the browser search for a ul tag with an a li:hover as ancestor. This doesn't have to be a direct parent, it can also be a grandparent, greatgrandparent, etc. Try to see if you understand why this holds for both menu levels. http://learn.shayhowe.com/html-css/getting-to-know-css/ might give you some understanding of how CSS selectors work

    A quick fix would be to change #centeredmenu ul li:hover ul into #centeredmenu ul li:hover > ul and to remove the #centeredmenu ul li:hover ul ul you have.

    I was playing with something like this today, see http://codepen.io/ckuijjer/pen/huyxn for my example. I tried to use mostly classes and hardly any element styling.