Designing a submenu with HTML and CSS

As a novice in web development, I recently attempted to create a website featuring a horizontal main menu with a vertical submenu.

However, I encountered an issue when hovering over a list item in the main menu - the submenu would drop down and disrupt the other elements of the main menu. Additionally, the submenu did not align correctly with the main menu list item.

You can find the code for this issue here: http://jsfiddle.net/mCvct/1/

I have spent some time trying to resolve this problem and exploring different solutions, but unfortunately, I have not been able to fix it yet.

Here is the code snippet:

<div id="menu">
    <ul>
        <li> <a href="#">SERVICES</a>

            <div class="subnavi">
                <ul>
                    <li><a href="#">service1</a>
                    </li>
                    <li><a href="#">service2</a>
                    </li>
                    <li><a href="#">service3</a>
                    </li>
                </ul>
            </div>
            <!-- end of subnavi -->
        </li>
        <li> <a href="contact.php">CONTACT US</a>
        </li>
        <li> <a href="#">HOME</a>
        </li>
        <li> <a href="#">ABOUT US</a>
        </li>
    </ul>
</div>
<!-- end of menu -->

    ul {
        overflow:hidden;
        vertical-align:middle;
    }
    ul li {
        list-style:none;
    }
    ul li a {
        width:16.2%;
        /*164.6px;*/
        font-size:1em;
        line-height:1.8em;
        float:left;
        display:inline;
        text-align:center;
    }
    #menu ul li .subnavi {
        display:none;
        clear:both;
    }
    #menu ul li .subnavi ul {
        width:20em;
        background:#a0a0a0;
        list-style:none;
        margin:auto;
        line-height:2em;
        border:1px solid #a60000;
    }
    #menu ul li .subnavi ul li a {
        width:24em;
        line-height:1.8em;
        float:none;
        display:inline;
    }
    #menu ul li:hover .subnavi {
        display:block;
    }

Answer №1

It is recommended to eliminate the use of clear:both in the .subnav and remove position: absolute from .subnav ul

#menu ul li .subnavi {
    display:none;
}
#menu ul li .subnavi ul {
    width:20em;
    background:#a0a0a0;
    list-style:none;
    margin:auto;
    line-height:2em;
    border:1px solid #a60000;
    position:absolute;
    top: 40px;
}

View this example for clarification.

Answer №2

Give this a shot

#menu ul li:hover .subnavi {
  display:block;
  position:absolute;
  top:40px;
    }

Check out the demo here

Answer №3

To properly position the sub navigation menu, you should add an absolute position to the .subnavi class and make the top of the ul element relative:

#menu ul li:hover .subnavi {
    position: absolute;
    top: 35px;
}
ul {
    position: relative;
}

Additionally, adding some padding to your links can prevent the sub navigation from closing prematurely when hovering over it with the cursor:

ul li a {
    padding: 10px;
}

You can see an example implementation at http://jsfiddle.net/pulleasy/mCvct/4/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Styling all HTML buttons globally without the need for a specific class, ID, or name attribute

To style all input tags in a different way based on their type, you can use the following CSS: input[type=button] { color:#333; font: italic 80% 'arial',sans-serif; background-color:#f0f0f0; border:2px dashed; } input[type=text] { color:#0 ...

The live search feature using AJAX is exceptionally sluggish

Update: I am utilizing XAMPP with the built-in Apache server and vscode. I have created a live search input functionality (HTML -> JavaScript -> PHP -> JavaScript -> HTML) that runs smoothly upon initial input. However, I have noticed that it ...

A quick guide on automatically checking checkboxes when the user's input in the "wall_amount" field goes over 3

I'm looking to have my program automatically check all checkboxes (specifically "Side 1, Side 2, Side 3 and Side 4") if the input for wall_amount is greater than 3. Is there a way to achieve this? I attempted lines 10-12 in JavaScript without success. ...

CSS: Flexible Sidebar - Dynamic Content

Can someone provide clarification on this visual illustration? To sum it up, I am looking to create a sidebar div that adjusts its width based on the content within, alongside a mainContent section that fills the remaining window width. ...

What could be causing the incorrect display of updates when my Grails checkbox onclick remote function Ajax call is triggered

Hi, I have a page named taskReminder that renders two templates: one for tasks and another for reminders. The task template includes a checkbox that, when checked, should update the task list. Everything is functioning correctly, but there seems to be an i ...

Adjusting the dimensions of the central container

Does anyone have suggestions on how to resize the middle red container when the window is resized, considering the fixed-width black containers on the left and right? I am aware that this can be achieved using jQuery by calculating the window width and a ...

How to set a background image using CSS in a Node.js application with Express

I'm currently delving into Node.js and as a total newbie, I haven't been successful in finding a solution. This is my directory structure: app --> public --> a.jpg & style.css When I provide access to these files in Express, it's ...

Navigation that sticks and changes upon hovering over div elements

Just delving into the world of jQuery and JS, so I appreciate your patience:) Currently, I have a sticky navigation bar positioned at the top of my webpage that links to different sections of content below. I am looking to create an effect where the corr ...

Experiencing a problem when switching between various divs on the website

Having difficulties with tracking the state of my application. Whenever I select a ticket on the edit form, the updated information is passed to all other tickets. I suspect there is an issue with how the state is being managed. The problematic function s ...

Node.js based simplistic static HTML web server

I'm looking to create a basic server for serving local HTML and JS files while working on development projects. I attempted to use a node app with express to respond with the page based on the URL, but unfortunately had no success. Here's my cod ...

It appears that the margin is malfunctioning

Let's cut to the chase - I want to add a chat sidebar to my website, but I'm having trouble adding margins to make it close in properly. Here's the code snippet: HTML <div class="inner-chat"> <div class="chat-box-messages"> ...

Having trouble with a Javascript function not executing upon form submission

I decided to give this tutorial a try: and I'm experimenting with it on my sandbox site here: But, when I click the submit button on the form to add a problem, nothing happens. There isn't even any output in the Chrome JavaScript console. I tr ...

Capturing the action phase in Liferay to change the cursor to 'waiting' mode

I'm currently working on a large Liferay project and have encountered a specific issue: Whenever something in the system is loading or processing, I need to change the cursor to a waiting GIF. While this is simple when using Ajax, there are many inst ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

Locate the class ID and refine the search results by another class

I am currently working on a task that involves extracting the first <li> element's id where it has the class name my_class, and checking if the <span> with the class Time contains a ":" using jquery. Below is the sample HTML structure: & ...

Tips for implementing custom styles on React components that have been imported locally

I'm currently struggling with applying CSS successfully to components imported from an npm library. My main challenge seems to be my use of css-loader in the react starter kit (https://github.com/kriasoft/react-starter-kit) without a full understandin ...

Troubleshooting issue with applying Select2 class dynamically with JavaScript

When developing my web application, I faced a challenge of dynamically adding new rows to a table using JavaScript. The row contains a <select> element which loads data from the ViewBag for its <options>. Additionally, I wanted to apply the Sel ...

Using Bootstrap 4 Modal with a touch of jquery-ui datepicker

Greetings! I have recently upgraded Bootstrap from version 3.3.X to 4.1 and am encountering an issue with modals not behaving as expected. Within a standard bootstrap Modal box on a page, the code looks like this: <!-- language:all lang-html --> &l ...

Can the "clear:right" property impact elements that are floated to the left?

Consider the following scenario: <div class="right">Box right 1</div> <div class="right">Box right 2</div> <div class="left">Box left</div> <p>... lengthy content ...</p> and this CSS code: .right { float ...

Having trouble with the CSS drop down menu in Internet Explorer 10?

Can you assist me with a challenge I am facing? I am currently working on creating a dropdown menu using only HTML and CSS. However, I have encountered an issue where the hover function is not functioning properly in IE10, although it works perfectly fine ...