0

I have already got a nav menu at the top of my page, but now I am trying to expand a sub menu when I hover over one of these options. My first idea was to simply have a "div" section of the code like such

<div id= "expanded_menu"> <!-- sub menu option --> </div>

and show/hide it based on whether it's nav option was hovered over, but then I realized that the submenu would disappear as soon as I took my mouse off of it's corresponding nav menu button. Does anyone know a way to hover over an option, have that bring up a menu, and then be able to access the submenu without it disappearing?

2

2 Answers 2

2

The usual way to do this is with nested lists...

<ul id="main-menu">
    <li>
        First menu item
        <ul class="sub-menu">
            <li>Sub menu item</li>
            ...
        </ul>
    </li>

    <li>Second menu item</li>
    ...
</ul>

And use the following CSS.

.sub-menu { display: none; }
#main-menu li:hover .submenu { display: block; }
Sign up to request clarification or add additional context in comments.

Comments

0

As long as the sub-menu is nested in the parent menu item's div, your method should work. So your HTML structure would be:

<div class="main_menu_item">MainItem
    <div class="sub_menu_item">Item1</div>
    <div class="sub_menu_item">Item2</div>
</div>

Then you're still hovering on the main item when hovering on the sub-items.

However, I personally would implement this all in CSS -- search for "CSS menus" and you'll find a ton of resources.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.