Search code examples
htmlcssnavsubmenumegamenu

how can I show the sub menu container on hover


How can I get it so when I hover over Menu item 2 it will show the submenu-container?

Codepen

Do I need to add something on the LI or the A tag? I have tried

ul li a:hover .submenu-container {
    display: block;
}

but it didn't work

 <ul>
    <li>
        <a href="#">Menu 1</a>
    </li>
    <li>
        <a href="#">Menu 2</a>
    </li>        

 </ul>
 <div class="submenu-container">
    <ul class="Sub-Menu">
           <h3>SubMenu 1</h3>
       <li>
           <a href="#">Sub-Menu 1</a>
       </li>
       <li>
           <a href="#">Sub-Menu 2</a>
       </li>        
    </ul>



* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.submenu-container {
    padding: 50px 20px;
    background-color: red;
    display: none;
}

ul {
    list-style: none;
}

ul li {
    margin: 10px 0;
}

ul li a {
    text-decoration: none;
    display: block;
    font-size: 1.2rem;
}

Your ideas are appreciated.

Many thanks

Paul


Solution

  • I would organize so that the submenu div is inside the menu 2 li and add:

    ul li:hover .submenu-container {
      display: block;
    }
    

    See the full working example here:

        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        
        .submenu-container {
            flex-wrap: nowrap;
            justify-content: space-between;
            padding: 50px 20px;
            background-color: red;
            display: none;
        }
    ul li:hover .submenu-container {
      display: block;
    }
        
        ul {
            list-style: none;
        }
        
        ul li {
            margin: 10px 0;
        }
        
        ul li a {
            text-decoration: none;
            display: block;
            font-size: 1.2rem;
        }
    <div class=wrap>    
    
    <ul>
            <li>
                <a href="#">Menu 1</a>
            </li>
            <li>
                <a href="#">Menu 2</a>
              <div class="submenu-container">
            <ul class="Sub-Menu">
                <h3>SubMenu 1</h3>
            <li>
                <a href="#">Sub-Menu 1</a>
            </li>
            <li>
                <a href="#">Sub-Menu 2</a>
            </li>        
        </ul>
      </div>
            </li>        
        </ul>
    
      </div>