Issue with mouseover functionality not functioning properly while hovering over a dropdown menu

My friend and I are relatively new to the world of coding with HTML, CSS, and a bit of JavaScript. We're currently facing an issue with our dropdown menu implementation. We have created buttons and added a dropdown menu using li and ul elements. Initially, we managed to prevent the dropdown menu from pushing down the content below it, but now we have encountered a problem when hovering over the "Products" button. The background of the dropdown menu blocks the mouseover action, resulting in the highlighted background of the button not showing completely and the hover effect not working on the entire button area.

ul {
  text-align: center;
  list-style: none;
  overflow: hidden:
}

ul li a {
  text-decoration: none;
  color: red;
  text-align: center;
  padding: 10px;
  font-size: 18px;
  float: left;
  text-decoration: none;
  font-weight: bold;
  font-family: Garamond, serif;
}

ul li ul {
  display: none;
  background-color: blank;
}

ul li a:hover {
  color: #fff;
  border-radius: 10px;
  background-color: blank;
  text-decoration: none;
  color: rgb(0, 204, 204);
  background: rgb(128, 128, 128);
  font-weight: normal;
  transition: all 0.21s ease-in-out;
}

ul li ul.dropdown {
  display: none;
  position: absolute;
  background-repeat: no-repeat;
  height: 0;
  transition: all 1s ease-in-out;
}

ul li:hover ul.dropdown {
  display: block;
  height: 200px;
  opacity: 1;
}

.navigation ul {
  display: flex;
}
<body>

  <div class="navigation">

    <ul>
      <li><a href="#"><span><ion-icon name="home-outline"></ion-icon></span><span> Home</span></a></li>&nbsp&nbsp&nbsp
      <li><a href="#"><span><ion-icon name="information-circle-outline"></ion-icon></span><span> About</span></a></li>&nbsp&nbsp&nbsp
      <li class="pro">
        <a href="#"><span><ion-icon name="bar-chart-outline"></ion-icon></span><span> Products &#9662;                      </span></a>&nbsp&nbsp&nbsp

        <ul class="dropdown">
          <br>
          <li><a href="#"><span>Laptops</span></a></li>
          <li><a href="#"><span>Monitors</span></a></li>
          <li><a href="#"><span>Printers</span></a></li>
        </ul>
      </li>
      <li><a href="#"><span><ion-icon name="mail-open-outline"></ion-icon></span><span> Contact</span></a></li>
    </ul>

  </div>

</body>

Answer №1

To ensure the dropdown menu does not obstruct the menu button, one approach is to position it at the bottom relative to the menu button using top:100%. This way, the dropdown will be displayed below the menu button.

It's important to note that when using absolute positioning, the dropdown is placed "relative to its closest positioned ancestor" (refer to position). To make sure the dropdown is properly positioned, I have included position:relative in the parent element of the dropdown, which is <li>, to establish a positioning context.

ul {
  list-style: none;
  padding: 0;
}

.navigation > ul {
  display: flex;
}

ul li a {
  display: block;
  color: red;
  padding: 10px;
  font-size: 18px;
  text-decoration: none;
  font-weight: bold;
  font-family: Garamond, serif;
}

ul li a:hover {
  border-radius: 10px;
  color: rgb(0, 204, 204);
  background: rgb(128, 128, 128);
  transition: all 0.21s ease-in-out;
}

.navigation > ul > li {
  position: relative;
}

.dropdown {
  display: none;
  position: absolute;
  top: 100%;
  transition: all 1s ease-in-out;
}

li:hover .dropdown {
  display: block;
}
<div class="navigation">
  <ul>
    <li><a href="#"><span><ion-icon name="home-outline"></ion-icon></span><span> Home</span></a></li>
    <li><a href="#"><span><ion-icon name="information-circle-outline"></ion-icon></span><span>About</span></a></li>
    <li>
      <a href="#"><span><ion-icon name="bar-chart-outline"></ion-icon></span><span>Products &#9662;</span></a>
      <ul class="dropdown">
        <li><a href="#"><span>Laptops</span></a></li>
        <li><a href="#"><span>Monitors</span></a></li>
        <li><a href="#"><span>Printers</span></a></li>
      </ul>
    </li>
    <li><a href="#"><span><ion-icon name="mail-open-outline"></ion-icon></span><span>Contact</span></a></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

Controlling 3D objects in a Three.js environment using a button

Can you assist a Three JS beginner with some code help? I'm looking to move rooms by clicking buttons, can someone guide me through this? 1| Button1 "increase + 1". Clicking Button 1 will move ROOM1 to ROOM2 and so on. When reaching Room8, ...

Sharing a variable with JavaScript within an OnClick event to update a progress bar in C#

When I click on a button, a JavaScript function is launched to create a progress bar. <asp:Button ID="btn_tel" runat="server" CausesValidation="False" OnClick="btn_telefono" Text="Check" CssClass="button" OnClientClick="move()" /></div> The f ...

Divide JSON arrays into separate select boxes

I have integrated AngularJS into certain sections of my website, rather than using it for the entire site. Currently, I am dealing with a scenario where I have a pair of select boxes, one dependent on the other. One box contains a list of countries, while ...

The setInterval function continues executing even after the page has been changed

I'm encountering an issue with my function where it continues to run even after the page has changed, resulting in an error. How can I go about stopping this behavior? Thank you! componentDidMount() { var current = 0; var slides = document.g ...

What is the best way to send an array of objects to a Jade template?

I'm looking to retrieve an array of objects from MongoDB and pass it to the client... Here is an example object: var objeto_img= { name:'name of the file', ...

What is the procedure for obtaining a Connect server's host name and port number?

Just like the example shown in this Express-related question, I'm wondering if there is a way to programmatically retrieve the host name and port number of a running Connect server? ...

Newbie mishap: Utilizing an array retrieved from a function in javascript/jquery

After submitting the form, I call a function called getPosts and pass a variable str through it. My aim is to retrieve the data returned from the function. // Triggered upon form submission $('form#getSome').submit(function(){ var str = $("f ...

Tips for initiating a component within a loop and terminating it after completing all 3 iterations

Looking for a solution to open and close tags in a loop every 3 iterations. The objective is to create a grid using container, row, and column elements. However, I am unsure how to achieve this. Example: render(){ const arrayName = ["john", " ...

Getting around CORS using PHP and JavaScript/Ajax

I've been grappling with this issue for hours, but my lack of expertise in web development has me stumped. Here's what I'm struggling with: There is another website that uses a script to fetch information like this: var url = "numbers. ...

The responsiveness of the container in Bootstrap 4 is not optimized for mobile devices

In one of my school projects, I decided to incorporate Bootstrap 4. The layout I created consists of a carousel positioned alongside some text. However, the issue lies in the fact that the carousel on the right is responsive across various device widths ex ...

Change a numeric value into a string within a collection of objects

Consider the following input array: const initialArray = [ { id: 1, name: 1 }, { id: 2, name: 2 }, { id: 3, name: 3 }, { id: 4, name: 4 } ]; The objective is to modify it ...

Tips on swapping out text with placeholder text like lorem ipsum

I am looking to replace my text with Lorem Ipsum. What I want is to input my text and receive back Lorem Ipsum that matches the length, case, and punctuation exactly. For instance, I, have a text that I want to replace. END; could turn into D, quam e ...

How to Trigger a Function in Backbone.js Upon Page Load

My function uses the simple selector $(.some-class) instead of this.$(.some-class). When I call this function in the render, it does not find the .some-class. It seems like the function needs to be called when the DOM is fully loaded. How can I achieve tha ...

Passing references in React to parent components

Struggling to adopt the react way of thinking, I'm facing an issue with invoking the .submit() method of the form component. Within a material-ui Dialog, buttons are passed via the actions property. Now, I need to trigger the .submit() method of the ...

Initiate the process of displaying data on a datetime chart using Highcharts

I am currently developing a yearly chart, but I've encountered a small issue. The chart begins in January, however there is no data available until May. The client specifically wants the chart to only display when there is data available, and unfortu ...

The div is incorrect and causing the label and input to move in the wrong direction

Whenever I try to adjust the position of the current-grade-name-input, the entire grade-point-input moves along with it inexplicably. /* final grade calculator: (wanted-grade - (current-grade * (1 - final%))) / final% Ex: have a 80 an ...

Retrieve and store an array from a JSON response using jQuery

Appreciate the assistance in advance. I am currently working with a plugin that requires a JSON response to be formatted as an array instead of an object. Below is a snippet of the JSON response I am receiving: { "instruments": [ { ...

Your request must be a POST method and only absolute URLs are allowed

I have created a client-side app.js file that makes a post request to a server (code provided below): const fetch = require('node-fetch'); /* Function for POSTing data */ const postData = async ( url = 'http://localhost/8000/add ...

Arrange a cluster of CSS table cells onto a fresh line

Currently, I am exploring CSS properties to create the visual appearance of a table using two levels of DOM elements. The highest-level element wraps around its child elements, which are styled to resemble a flat list. For example: <div class="t"> ...

What is the best way to interact with a checkbox that has a label using Selenium in Node.js?

My HTML document contains multiple checkbox classes with different texts for each checkbox name. Here is a snippet of the HTML code: <div class="col-md-12 syllabus-div-1"> <h4 class="vertical-spacing">Coaching<i class="fa fa-graduation- ...