Tips for positioning and styling submenu on dropdown using CSS

I am facing an issue with my navigation menu in the design. The sub menu is not displaying when hovered. Is there a solution to fix this problem? Could it be due to missing CSS styles?

nav ul {list-style-type: none; overflow: hidden; background: #000; position: relative;}
nav li {float: left;}
nav li > a {padding: 15px 15px; display: inline-block; text-decoration: none; color: white; text-align: center;}
nav li > a:hover {background: violet;}

nav ul ul { position: absolute; background: green; top: 100%;}
nav ul ul li > a:hover {color: red;}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>sample UL</title>
    <style media="screen">

    </style>
  </head>
  <body>
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu 1</a></li>
        <ul>
          <li><a href="#">submenu 1</a></li>
          <li><a href="#">submenu 2</a></li>
        </ul>
      </ul>
    </nav>
  </body>
</html>

Answer №1

Your code contains multiple errors, with the most critical one being the top: 100% styling on the submenu container, causing it to move outside the visible area.

In addition, it is crucial to set the submenu header li as position: relative, and the submenu ul should have a default display: none property which changes to display: block on hover. It's also essential to place the submenu container inside the respective li acting as its header. Check out the corrected code below:

nav ul {
  list-style-type: none;
  background: #000;
}

nav li {
  display: inline-block;
  position: relative;
}

nav li>a {
  padding: 15px 15px;
  display: inline-block;
  text-decoration: none;
  color: white;
  text-align: center;
}

nav li>a:hover {
  background: violet;
}
nav ul li ul {
  display: none;
  position: absolute;
  top: 45px;
  left: 0px;
  width: 100px;
  overflow: visible;
  padding: 0;
}
nav ul li:hover ul {
  display: block;
}
nav ul ul li {
  background: green;
  display: block;
}
nav ul ul li>a:hover {
  color: red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>sample UL</title>
  <style media="screen">

  </style>
</head>

<body>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Menu 1</a>
        <ul>
          <li><a href="#">submenu 1</a></li>
          <li><a href="#">submenu 2</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</body>

</html>

Answer №2

nav ul {
            list-style-type: none;
            overflow: hidden;
            background: #000;
            position: relative;
        }

        nav li {
            float: left;
        }

        nav li > a {
            padding: 15px 15px;
            display: inline-block;
            text-decoration: none;
            color: white;
            text-align: center;
        }

        nav li > a:hover {
            background: violet;
        }

        nav ul ul {
            background: green;
            top: 100%;
        }

        nav ul li a + ul {
            display: none;
        }

        nav ul li a:hover + ul {
            display: block;
        }
        
        .sub-menu:hover {
            display: block;
        }
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>sample UL</title>
</head>
<body>
<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu 1</a>
            <ul class="sub-menu">
                <li><a href="#">submenu 1</a></li>
                <li><a href="#">submenu 2</a></li>
            </ul>
        </li>
    </ul>

</nav>
</body>
</html>

Answer №3

Give this a shot and see what you can pick up from it.

<!DOCTYPE html>
<html>
<head>
<style>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: red;
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}
</style>
</head>
<body>

<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

</body>
</html>

Answer №4

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>example Unordered List</title>
    <style media="screen">
    </style>
  </head>
  <body>
    <nav class="clearfix">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu 1</a>
          <ul class="submenu">
            <li><a href="#">submenu 1</a></li>
            <li><a href="#">submenu 2</a></li>
          </ul></li>
      </ul>
    </nav>
  </body>
</html>

For detailed explanation, see this Clearfix guide

Add "clearfix" class to nav and "submenu" class to inner ul as mentioned in the previous text

.clearfix:before, .clearfix:after {
    content:"";
    display:table;
}

.clearfix:after {
    clear:both;
}

.clearfix {
    zoom:1;
}

nav {
    background: #333A31;
    height: 2.3em;
}
ul, li {
    margin: 0;
    padding: 0;
    list-style: none;
    float: left;
}
ul {
    background: #D5333C;
    height: 2em;
    width: 100%;
}
li {
    position: relative;
}
li a {
    display: block;
    line-height: 2em;
    padding: 0 1em;
    color: white;
    text-decoration: none;
}
li a:hover {
    background: #9155311;
    height: 2em;
    padding-top: .3em;
    position: relative;
    top: -.3em;
    border-radius: .3em .3em 0 0;
}
.current, a:hover.current {
    background: #AD9B7F;
    color: #eee;
    padding-top: .3em;
    border-radius: .3em .3em 0 0;
    position: relative;
    top: -.3em;
    border-bottom: .3em solid #917F63;
    cursor: default;
}
/*dropdown menu styles*/
ul.submenu {
    float: none;
    background: #916A31;
    width: auto;
    height: auto;
    position: absolute;
    top: 2em;
    left: -9000em;
}
ul.submenu li {
    float: none;
}
nav li:hover ul {
    left: 0;
}
ul.submenu li a {
    border-bottom: 1px solid white;
    padding: .2em 1em;
    white-space: nowrap;
}
ul.submenu li:last-child a {
    border-bottom: none;
}
ul.submenu li a:hover {
    background: #D5973C;
    height: 2em;
    padding-top: .2em;
    top: 0;
    border-radius: 0;
}

Hope this explanation is helpful.

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

Implementing class changes based on scroll events in JavaScript

const scrollList = document.getElementsByClassName('scrollList'); function scrollLeft() { scrollList.scrollLeft -= 50 } function scrollRight() { scrollList.scrollLeft += 50 } #scrollList { display: flex; overflow: auto; width: 10 ...

I'm having trouble locating the corresponding end tag for "<%". How can I resolve this issue?

Here lies the issue: <% for(let i = 0; i < <%= elements %>.length; i++){ %> <li><%= elements[i] %></li> <%}%> ...

What is the best way to retrieve the elements stored within the 'this' object I am currently manipulating?

How can I access the elements nested within the 'this' that I am currently operating on? Below is the HTML code that I am currently working with: <div class="expander" id="edu">educational qualifications <ul class="list"&g ...

Unusual characters found in the fields of a Postgresql database table

In six out of over 30 fields in one of my postgresql tables, an unusual character has been found at the end of user-input text. This data is entered into the table through a PHP web form that has been in use for years. It's puzzling to see this charac ...

I am experiencing an issue where certain HTML classes are not applying the CSS code properly within my PHP file

I am facing an issue with my code where some of the HTML classes are not applying the CSS styles correctly, such as the first class "class="foot"". Below are the files that I have: * { margin: 0%; padding: 0%; ...

Formatting issue with selecting checkboxes in Primefaces DataTable filter menu

I decided to replicate the Primeface showcase for filters on my Local JBoss installation by referring to the link provided below: http://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml. However, I encountered an issue where my selectCheckboxMenu ...

Visual feedback: screen flashes upon clicking to add a class with jQuery

I have successfully added a click event to my pricing tables in order to apply an animation class on mobile devices. However, I am facing an issue where every time I click on a pricing option on my iPhone, the screen flashes before the class is applied. Is ...

What is the best way to dynamically swap out an image within an element?

Is there a way to dynamically replace this image without deleting the HTML SVG element and re-rendering it? xlink:href="http://missosology.info/forum/download/file.php?avatar=11666_1307312313.jpg" CODE: https://jsfiddle.net/bfv17f0e/ <svg class="clip ...

Instructions for adding and deleting input text boxes on an ASP.NET master page

*I am currently facing an issue with adding and removing input textboxes for a CV form using ASP.NET in a master page. Whenever I click on the icon, it doesn't seem to work as intended. The idea is to have a textbox and an icon "+" to add more textbox ...

Django CSS not loading properly on webpage after initial load

I am having trouble understanding why the main.css file is not loading properly in my Django project. I am using the skeleton boilerplate and trying to change the background color, but the changes are not reflecting on the webpage. It's strange becaus ...

Most Effective Method for Switching CSS Style Height from "0" to "auto"

After coming across a question with no answer that suited my needs, I decided to share the solution I created. In my case, I had a hidden generated list on a page which was initially set with a CSS height of "0" and then expanded upon clicking by transit ...

Troubleshooting Problem with Responsive Bootstrap Navbar

I've been working on creating a menubar using Bootstrap where the logo image is centered instead of being on the left side of the bar. However, I'm facing an issue where the right links in the menu go off the screen. When I view the page with a w ...

A method designed to accept an acronym as an argument and output the corresponding full name text

Having trouble with my current task - I've got an HTML file and external JS file, and I need a function that takes an element from the 'cities' array as input and returns a string to be used in populating a table. I've set up a functio ...

What is the best method to make a hyperlink within an IFrame open in a new window?

Let me share the code I've been working on: <!DOCTYPE html> <html> <head> <base target="_blank"> </head> <body> <div style="border: 2px solid #D5CC5A; overflow: hidden; margin: 15px auto; max-width: 800px; ...

How can I retrieve the value of a specific <span> element by referencing the class of another <span> within

I have come across the following HTML: <div class="calculator-section"> <p> <span class="x"></span> <span class="amount">30</span> </p> <p> <span class="y"></span ...

Unable to supersede CSS module styling using global or external stylesheets in React, Next.js, or React Native

I'm facing a challenge finding a solution to a basic issue. I have a component that is initially styled using a class from a *.module.css file. However, I want to replace this style with one from a global stylesheet or another stylesheet but I can&apo ...

I keep receiving unexpected security alerts in Firefox without any specific cause

After making some updates to my page, I started receiving a security warning. The data you've entered may be at risk as it is being sent over an insecure connection that could potentially be intercepted by a third party. I'm puzzled because m ...

implementing a vertical separator in the main navigation bar

Is there a way to insert a vertical divider after each li element on the main menu? <ul class="nav navbar-nav"> <li><a href="#"><i class="fa fa-home"></i> <span class="sr-only">(current)</span></a>< ...

Is Next.js experiencing issues with animating presence specifically for exit animations?

I'm facing an issue with adding an exit animation to my components in next js. Despite setting an initial animation, the exit animation doesn't seem to work as expected. Could someone please help me figure out what I'm doing wrong here? Be ...

The Date Filter is causing a glitch in formatting the date value

I have a variable called dateSubmitted with the value of "dateSubmitted": "07-09-20:11:03:30" Currently, I am utilizing Angular Version 7 Within my HTML code, I am using the date filter to format the date like so: <td> {{element.dateSubmi ...