I am experiencing an issue where the submenu does not appear when I hover over the elements in the navigation bar

How can I make the submenu appear when hovering over the corresponding navigation element on the navigation bar? Here is how I set the display to none for the second level navigation list:

nav ul ul { 
    position:absolute; top: 100%;
    background-color: #2b0306; 
    display: none;
}

I also set the first navigation list to display as inline-block like this:

nav ul li a {
    display: inline-block;
    color: #fff;
    padding: 10px 20px;
    text-decoration: none;
    width: 250px;
    position: relative;
} 
nav ul li a:visited { color: #fff; }
nav ul li a:hover { background-color: #6d0911; }
nav ul ul { position:absolute; top: 100%; background-color: #2b0306; display: none; }
nav ul ul li { position: relative; }
nav ul ul ul { left: 100%; top: 0px; }

For the nav position, I used absolute positioning:

nav {
    background-color: rbga(0,0,0,.65);
    position: absolute;
    top: 0px; left: 0px;
    padding: 50px 0 0 0;
    width: 100%;
}
nav::after { content: ' '; display: block; clear: both; }
nav ul { list-style: none; margin: 0; padding: 0px; }
nav ul li: hover { background-color: #2b0306; }
nav ul li: hover > ul { display: block; }

/* top-level */
nav > ul { padding-left: 300px; }
nav > ul > li { float: left; }
nav > ul > li > a { 
width: auto; 
padding: 10px 20px 15px 20px; 

}

And here is the HTML code snippet:

<nav>
        <ul><!--first level navigation-->
            <li><a title="About Us" href="aboutATMC.php" >About Us</a></li>
            <li>
                <a title="Services" href="#" aria-haspopup ="true">Services</a>
                <ul><!--Second level navigation-->

                    <li><a title="Consultancy" href="#">Consultancy</a></li>
                    <li>
                        <a title="Learning &amp; Development Solutions" href="#" aria-haspopup ="true">Learning &amp; Development Solutions</a>
                            <ul><!--Third level navigation-->
                                <li><a title="Training &amp; Coaching" href="#">Training &amp; Coaching</a></li>
                                <li><a title="Learning Material" href="#">Learning Material<& Understandable enough?</p>
    </div>

Any suggestions or feedback are greatly appreciated.

Answer №1

Here is a suggestion to improve your menu design:

/* custom top and sub menu */
nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
nav li a {
  padding: 10px;
  display: inline-block;
}
nav > ul > li {
  display: inline-block;
}

/* styling for subpages in the menu */
nav > ul > li > ul {
  display: none;
  position: absolute;
  background-color: #ccc;
}
nav ul li:hover {
  background-color: #ccc;
}
nav ul li:hover ul {
  display: block;
}
<nav>
  <ul>
    <li>
      <a href="#">Home</a>
      <ul>
        <li><a href="#">Subpage 1</a></li>
        <li><a href="#">Subpage 2</a></li>
        <li><a href="#">Subpage 3</a></li>
      </ul>
    </li>
    <li>
      <a href="#">About</a>
    </li>
  </ul>
</nav>

Answer №2

In this segment, I have incorporated some HTML code for demonstration purposes.

<nav>
    <ul>
        <li>
            <a href='#'>Test</a>
            <ul id='submenu'>
                <li>Submenu Test</li>
                <li>Submenu Test</li>
                <li>Submenu Test</li>
            </ul>
        </li>
        <li><a href='#'>Test</a></li>
        <li><a href='#'>Test</a></li>
    </ul>
</nav>

<style>
    nav {
        background-color: rgba(0,0,0,.65);
        position: absolute;
        top: 0px; left: 0px;
        padding: 50px 0 0 0;
        width: 100%;
    }

    nav ul ul {
        position:absolute;
        top: 100%;
        background-color: #2b0306;
        display: none;
    }

    nav ul li:hover > #submenu {
        display: block;
    }

    nav ul li a {
        display: inline-block;
        padding: 10px 20px;
        text-decoration: none;
        width: 250px;
        position: relative;
    }
</style>

If you notice the box appearing on hover, then everything is working correctly!

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

Encountering difficulties accessing image files from .css in Jenkins

Upon visiting the URL provided below, I was able to successfully integrate a .css file into my Jenkins project. Everything appeared to be working properly until I logged out. Upon logging out of the Jenkins page, I noticed that my css file images were no l ...

React Material Design Cards for Responsive Layouts

Hi there, I am currently navigating my way through Material Design and attempting to ensure that the cards section is responsive by utilizing media queries and flex. However, I have encountered an issue where only a portion of the image is displayed when t ...

"Render Failure" JavaScript and CSS files

I'm encountering a peculiar issue while attempting to load certain JS and CSS files. Within my ASP.NET MVC Web Project, I have an Index.html file where I've specified the loading of script files. It's worth noting that I have modified the U ...

Fixing the vertical centering of content when printing HTML

My bootstrap 4-based HTML page is looking good on most screens, but when I try to print it, the content is vertically centered in the print layout. How can I ensure that the content is displayed at the top of the paper? https://i.sstatic.net/kfjrx.png Be ...

What is the best way to ensure that a block level element with a fixed or absolute position stays within the bounds of the page and automatically adjusts if the edges start to

In my current project, I am working on adding a new feature to the existing page. The challenge is to include an element that will be displayed above or below a button, which will serve as a toggle to open or close it. This element needs to be triggered by ...

A section element is covering a div in HTML

Currently facing some layout issues while trying to organize a page using sections. Here's the code snippet I'm working with... https://jsfiddle.net/ytv4zp4b/ <section> <div class="width"> Test Content &l ...

What is the reason behind getComputedStyle having visibility set to visible?

What is the reason behind getComputedStyle always returning element visibility as visible even when no explicit visibility has been set? For instance: getComputedStyle($('#block1')[0],null).visibility; --- "visible" Meanwhile: $('#block1&a ...

Not all properties are affected by the CSS stylesheet

Even though my CSS stylesheet successfully changes the background color of the page, I am facing issues with other properties that I have set on my HTML tags. {% block body %} <div id='title'> <h1> VISUALIZER2D ...

CSS hover effect with multiple image overlays on one area upon mouseover

I've been working on a map project that involves changing the color of multiple areas upon hovering using CSS. I've set up the main div with position:relative, and positioned the images inside absolutely to create hover effects by transitioning o ...

css content exceeds div boundaries

Hello, I am experiencing an issue with my webpage located at . When I zoom out, the content overflows from its containing div. Setting the height to auto or 100% works fine, but the left column ends up being smaller than the right one, and using clear do ...

Learning HTML and CSS integration through Codecademy is a fundamental part of the course

After following the instructions from an old HTML and CSS introduction on , I successfully created my first website. However, the code works perfectly on a special constructor page, but once I try to view it on a browser, the HTML and CSS don't seem t ...

Creating a pagination feature for an HTML table without using jQuery

Hello, I am relatively new to front-end development and currently undergoing training in a project involving HTML, CSS, and JavaScript. However, I seem to be facing an issue with the Table HTML element. I have created a table structure below: A header ...

How to place an image in the bottom right corner using CSS

Does anyone know how to position an image in the bottom right corner of a div? I have tried using margin-right and padding-right with 0px on the img tag, but it does not seem to work. The black lines indicate that I do not need that space Here is my Cod ...

What is the best way to navigate through a series of images using left and right arrows in a React

I need help implementing a feature where I can slide horizontally through a collection grid of 10 movie items per row, similar to Netflix grids. Is there an easy way to achieve this with arrows on the right and left ends? <div className="collection-p ...

The div has extra white space at the bottom due to the Hide/Show content feature

Having trouble stretching a scrolling div to 100% of its parent container's height? The Hide/Show content feature is causing whitespace at the bottom of the div. Check out the jsfiddle here: http://jsfiddle.net/fkvftff2/1/ LATEST UPDATE: The issue a ...

Responsive HTML code for a textarea, button, and table

I'm currently developing a website that includes a textarea, button, and table within the body content. The responsiveness of my content works well, except for extremely narrow displays such as mobile phones. In such cases, the table does not wrap pr ...

The CSS property for restricting white-space to nowrap is not functioning as

Having a div with multiple child divs floating left can be tricky. To prevent them from breaking, setting them to display:inline-block and white-space:nowrap seems like the solution. However, in some cases this may not work as expected. If your goal is to ...

Reduce the size of a webpage by 10%

Have you ever noticed that when you press Ctrl+- on any browser, the page scales down to 90% of its original size? It actually looks nicer at 90%, so I want my webpage to default to opening at 90% instead of 100%. I've tried following common approach ...

Is it possible to conceal specific sections of a timeline depending on the current slide in view?

For my product tour, I've structured a timeline with 4 main sections, each containing 4-5 subsections. Here's how it looks: <ul class="slideshow-timeline"> <li class="active-target-main main-section"><a href="#target">Targe ...

Circular center button in the footer UI

Struggling with aligning a button in the footer perfectly? I have two icons on each side and while the UI is almost there, something seems off with the center icon's padding and shadow. I'm currently working with material-ui. Take a look at the i ...