Problem with items overlapping in hoverable drop-down menu

I have been attempting to create a hoverable dropdown menu, but I am encountering an issue where the items of the dropdown are overlapping. Despite my efforts to adjust the CSS classes, I have not been successful in fixing this problem.

Additionally, I have tried modifying the display settings for the links with no success. Below is the code snippet that I have written:

<style>
#menu
{
   margin:0;
   font-size: 30px;
   border-bottom: 1px solid;
   text-align: center;
}

#menu a
{
   color:#900;
   text-decoration:none;
}

#menu .subitem a{
   display: block;
   padding: 12px 16px;
   z-index: 1;
  background-color: #f1f1f1;
  border-bottom: 1px solid;
}

#menu a:hover
{
   text-decoration:underline;
}

.item{
   position: relative;
   display: inline-block;
   border-right: 0.5px solid;
   padding-right: 30px;
   padding-left: 30px;
}

.subitem{
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  left: 0;
  z-index: 1;
}

#menu .item:hover .subitem{
    display: block;
}
</style>
<div id="navWrapper">
      <ul id="menu">
         <li class="item"><a href="#">Small Things</a>
            <ul class="submenu">
               <li class="subitem"><a href="#">Gnomes</a></li>
               <li class="subitem"><a href="#">Fairies</a></li>
               <li class="subitem"><a href="#">Elves</a></li>
               <li class="subitem"><a href="#">Leprechauns</a></li>
            </ul>
         </li>
         <li class="item"><a href="#">Big Things</a>
            <ul class="submenu">
               <li class="subitem"><a href="#">Loch Ness Monster</a></li>
               <li class="subitem"><a href="#">Ogres</a></li>
               <li class="subitem"><a href="#">Giants</a></li>
               <li class="subitem"><a href="#">Dragons</a></li>
            </ul>
         </li>

      </ul>
  </div>

I am aiming to achieve the desired display of each item when hovering with the mouse as shown in this image.

Answer №1

You mixed up your .submenu and .subitem:

#menu
{
   margin:0;
   font-size: 30px;
   border-bottom: 1px solid;
   text-align: center;
}

#menu a
{
   color:#900;
   text-decoration:none;
}

#menu .subitem a{
  display: block;
  padding: 12px 16px;
  z-index: 1;
  background-color: #f1f1f1;
  border-bottom: 1px solid;
}

#menu a:hover
{
   text-decoration:underline;
}

.item{
   position: relative;
   display: inline-block;
   border-right: 0.5px solid;
   padding-right: 30px;
   padding-left: 30px;
}

.subitem {
  /* display: none; <- remove that */
  /* position: absolute; <- remove that, or all subitems will be in top left corner */
  background-color: #f1f1f1;
  min-width: 160px;
  left: 0;
  z-index: 1;
}

.submenu {
  position: absolute; /* <- place it here */
  display: none; /* <- place it here */
  list-style-type: none;
  left: -40px;
  top: 30px;
}

#menu .item:hover .submenu {
  display: block;
}
<div id="navWrapper">
      <ul id="menu">
      
         <li class="item"><a href="#">Small Things</a>
            <ul class="submenu">
               <li class="subitem"><a href="#">Gnomes</a></li>
               <li class="subitem"><a href="#">Fairies</a></li>
               <li class="subitem"><a href="#">Elves</a></li>
               <li class="subitem"><a href="#">Leprechauns</a></li>
            </ul>
         </li>
         
         <li class="item"><a href="#">Big Things</a>
            <ul class="submenu">
               <li class="subitem"><a href="#">Loch Ness Monster</a></li>
               <li class="subitem"><a href="#">Ogres</a></li>
               <li class="subitem"><a href="#">Giants</a></li>
               <li class="subitem"><a href="#">Dragons</a></li>
            </ul>
         </li>

      </ul>
  </div>

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

Is it possible to swap images by clicking on them?

Let's say I'm working with 3 images. Image A, B and C. A is the main image initially. If I click on image B, it will become the main image and image A will take its place in the old position. The same condition applies when B is the main image a ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

Is it possible to delete an element from both local storage and HTML by referencing its ID?

Experience a simple flashcard game where you enter a question and answer to create a new flash card stored as an object within the cards array. The newly created flash card is also displayed by appending a new element to the flash cards section on the webp ...

CSS responsive grid - adding a horizontal separator between rows

I currently have a responsive layout featuring a grid of content blocks. For desktop view, each row consists of 4 blocks. When viewed on a tablet, each row displays 3 blocks. On mobile devices, each row showcases 2 blocks only. I am looking to add a ho ...

Nested Divs in CSS

I'm really struggling to get a div to display under another div in the way they usually do. Currently, I am using MaterializeCSS for my layout setup: <div class="container valign-wrapper"> <div class="customClass"></div> &l ...

Tips for modifying the color of selected text

Is there a way to change the color of the dropdown text? Currently, it is set to red but initially appears as black. When clicked on, it changes to red. How can I make it so that the color is initially displayed as red? <select> <option style ...

Extract CSS from Chrome developer tools and convert it into a JavaScript object

Recently, we have started incorporating styles into our React components using the makeStyles hook from Material-UI. Instead of traditional CSS, we are now using JavaScript objects to define styles. An example of this is shown below: const useStyles = ma ...

Get the values of var1 and var2 from the URL in PHP, for example: `example.php?var1

Currently, a portion of my website operates by using GET requests to navigate to profiles or pages. However, I am concerned about the scenario where a user visits someone's profile page (requiring one GET) and then clicks on a sub-tab within that prof ...

Creating a Sudoku puzzle grid with HTML and CSS - a step-by-step guide

Apologies for the basic inquiry, but I have created a sudoku grid with the following structure. Modified(Using Tables): <table id="grid" border="1" style="border-collapse: collapse;"> <tr class="row"> ...

Design an arrangement using div elements and CSS styling

Creating a layout for a website can be challenging. I am using three div tags - container, left, and right. My goal is to have the left side fixed-width for navigation, while the right side displays dynamic content loaded from a database. However, I'm ...

Looping through multi-dimensional JSON objects with jQuery

Hello there, I'm currently facing some challenges in getting the screen below to work properly: Project progress screen I have generated the following JSON data from PHP and MYSQL. My goal is to display the project alongside user images and names whe ...

CSS or Coding? Learn how to display items side by side instead of on top of each other

Hey there! I manage a music website that is currently in beta. I'm running into an issue with the way the music results are appearing in the bottom player - they're all stacked on top of each other instead of being listed side by side. I've ...

Enhancing bar border with the addition of a separator

Our current situation is similar to this: https://i.stack.imgur.com/Luj1S.png but our goal is to have a chart with separators on both sides of the age brackets, like this: https://i.stack.imgur.com/03UpO.png In order to achieve this, we have utilized t ...

The process of sending a file and then showcasing it once it has been received

I'm having trouble uploading a file to my php server and displaying the image to the user. Despite printing out what seems to be the correct file destination with echo($fileDestination);. Below is the HTML code: <!DOCTYPE html> <html> ...

A guide on customizing the appearance of individual items in a vue v-for loop based on specific conditions

I am currently developing a multiple choice quiz game and I want the selected answer by the user to change color, either red or green, depending on its correctness. To achieve this, I have created a variable called selected that correctly updates when the ...

Utilize the <a> element as a button to submit the data form

I am looking to transfer data from a form to another PHP page without using a button within the form itself. Instead, I have placed a separate button outside of the form for submission. How can I achieve this by sending the form data to the other page? Bel ...

How can CSS be used to make an element completely move off the visible screen?

Imagine if I had a div that was positioned outside of the visible area of the computer monitor screen. When using CSS transitions to move it to a specific position, it would create an effect of the element slowly moving in from outside of the screen. Add ...

Angular 14: Exploring the left datepicker panel navigation in PrimeNG

In my situation, I am trying to adjust the position of the date-picker in relation to a panel displayed within a <p-calendar> element. Due to a splitter on the right side, I need to shift the date-picker towards the left. Here is the HTML code: < ...

What is the process for verifying that a checkbox is unchecked through the use of Ajax and PHP?

I am working with a checkbox element in my form. Here is the code: <input type="checkbox" value="yes" id="checkBox"> This checkbox is part of my form, and I am sending the form data to a PHP file using Ajax: var check = $('#checkBox').v ...

The CSS transition will only activate when coupled with a `setTimeout` function

After using JavaScript to adjust the opacity of an element from 0 to 1, I expected it to instantly disappear and then slowly fade back in. However, nothing seems to happen as anticipated. Interestingly, if I insert a setTimeout function before applying th ...