How come the Hover effect is activated even without hovering the mouse over the text?

I have designed a dropdown menu, but I am facing an issue with the portfolio section. When I hover over the portfolio, the dropdown menu appears correctly. However, when I hover over the text of the first dropdown menu, all sub-dropdown menus from other dropdowns appear in succession.

Another problem arises in the Contact section. If I add a small bottom margin between each dropdown menu and then hover over the Contact text, the dropdown icon displays. But as soon as I move my mouse from the text to the dropdown icon, the dropdown menu disappears.

Below is my complete code:

/* CSS for the menu */

.menu {
  text-align: center;
}

.menu ul {
  list-style: none;
}

.menu ul li {
  display: inline-block;
  background: #545454;
  color: white;
  padding: 15px 55px;
  margin-left: -5px;
  border-left: 1px solid black;
  font-family: 'Balsamiq Sans', cursive;
  font-size: 18px;
  position: relative;
}

.menu ul li i {
  position: relative;
  right: 10px;
}

.menu ul li:hover {
  background-color: rgb(216, 49, 49);
  transition: .8s;
  cursor: pointer;
}


/* Dropdown menu styling */


/* Contact section */

.menu ul li .contact {
  position: absolute;
  left: 38px;
  top: 100%;
  width: 0px;
  display: inline-block;
  padding: 0;
}

.menu ul li .contact li {
  width: 0px;
  display: inline-block;
  line-height: 16px;
  border-radius: 50%;
  visibility: hidden;
  opacity: 0;
  transform: scaleY(0);
  transform-origin: top;
}

.menu ul li .contact li i {
  font-size: 50px;
  position: relative;
  top: 0px;
  right: 25px;
}

.menu ul li .contact li:first-child:hover {
  background-color: royalblue;
}
... 
<!DOCTYPE html>
<html lang="en">

<head>

  <title>Menu</title>
  <link rel="stylesheet" href="./dropdown menu.css">

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">

  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Balsamiq+Sans:ital@1&display=swap" rel="stylesheet">

</head>

<body>
  <div class="menu">
    <ul>
      <li><i class="fas fa-home"></i>Home</li>
      <li><i class="fas fa-female"></i>About</li>
      <li><i class="fas fa-user-edit"></i>Contact
        <ul class="contact">
          <li><i class="fab fa-facebook-f"></i></li>
          <li><i class="fab fa-instagram"></i></li>
          <li><i class="fab fa-youtube"></i></li>
          <li><i class="fab fa-twitter"></i></li>
          <li><i class="fab fa-linkedin-in"></i></li>
        </ul>
      </li>
      <li><i class="fab fa-wordpress-simple"></i>Portfolio
        <ul class="portfolio">
          <li>Work no 1
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 2
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 3
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 4
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 5
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
        </ul>
      </li>
      <li><i class="fas fa-sign-in-alt"></i>Login</li>
    </ul>
  </div>
</body>

</html>

Answer №1

When you opt not to utilize the traditional display:none/block method, your concealed elements occupy space within their parent element. As a result, hovering over these spaces triggers the parent element's :hover effect.

To circumvent using display:none/block, consider employing pointer-events:none/auto on the ul child instead.

Below is a sample of the code in question:

/* menu section */

.menu {
  text-align: center;
}

.menu ul {
  list-style: none;
}

.menu ul li {
  display: inline-block;
  background: #545454;
  color: white;
  padding: 15px 55px;
  margin-left: -5px;
  border-left: 1px solid black;
  font-family: 'Balsamiq Sans', cursive;
  font-size: 18px;
  position: relative;
}

.menu ul li i {
  position: relative;
  right: 10px;
}

.menu ul li:hover {
  background-color: rgb(216, 49, 49);
  transition: .8s;
  cursor: pointer;
}


/* dropdown menu section */


/* contact section */

.menu ul li .contact {
  position: absolute;
  left: 38px;
  top: 100%;
   
  display: inline-block;
  padding: 0;
}

.menu ul li .contact li {
  width: 0px;
  display: inline-block;
  line-height: 16px;
  border-radius: 50%;
  visibility: hidden;
  opacity: 0;
  transform: scaleY(0) translate(-1em);
  transform-origin: top;
}
  .menu ul li .contact li ul {
  /* Additional styling */ pointer-events: none;  
}

.menu ul li .contact li i {
  font-size: 50px;
  position: relative;
  top: 0px;
  right: 25px;
}

.menu ul li .contact li:first-child:hover {
  background-color: royalblue;
}

.menu ul li .contact li:nth-child(2):hover {
  background-color: rgb(190, 201, 37);
}

.menu ul li .contact li:nth-child(3):hover {
  background-color: rgb(228, 22, 22);
}

.menu ul li .contact li:nth-child(4):hover {
  background-color: rgb(1, 130, 250);
}

.menu ul li .contact li:last-child:hover {
  background-color: rgb(66, 110, 241);
}

.menu ul li:hover .contact  li {
  visibility: visible;
  opacity: 1;
  transition: .5s;
  transform: scaleY(1) translate(-1em);
  cursor: pointer;
  }

.menu ul li:hover ul {
  /* Additional styling */ pointer-events: auto;
 
}


/* portfolio section */

.menu ul li .portfolio {
  position: absolute;
  top: 100%;
  left: 0px;
  padding: 0;
  margin-left: 5px;
}

.menu ul li .portfolio li {
  width: 84px;
  position: relative;
}

.menu ul li .portfolio li ul {
  position: absolute;
  left: 100%;
  top: 0px;
  padding: 0;
  /* Additional styling */ pointer-events: none;
}

.menu ul li .portfolio li ul li {
  visibility: hidden;
  opacity: 0;
  margin-left: 0px;
  transform: scaleX(0);
  transform-origin: center;
}

.menu ul li .portfolio li:hover ul li {
  visibility: visible;
  opacity: 1;
  transform: scaleX(1);
  transition: .8s;
  /* Additional styling */ pointer-events: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>

  <title>Menu</title>
  <link rel="stylesheet" href="./dropdown menu.css">

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">

  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Balsamiq+Sans:ital@1&display=swap" rel="stylesheet">

</head>

<body>
  <div class="menu">
    <ul>
      <li><i class="fas fa-home"></i>Home</li>
      <li><i class="fas fa-female"></i>About</li>
      <li><i class="fas fa-user-edit"></i>Contact
        <ul class="contact">
          <li><i class="fab fa-facebook-f"></i></li>
          <li><i class="fab fa-instagram"></i></li>
          <li><i class="fab fa-youtube"></i></li>
          <li><i class="fab fa-twitter"></i></li>
          <li><i class="fab fa-linkedin-in"></i></li>
        </ul>
      </li>
      <li><i class="fab fa-wordpress-simple"></i>Portfolio
        <ul class="portfolio">
          <li>Work no 1
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 2
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 3
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 4
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
          <li>Work no 5
            <ul>
              <li>sample 1</li>
              <li>sample 2</li>
            </ul>
          </li>
        </ul>
      </li>
      <li><i class="fas fa-sign-in-alt"></i>Login</li>
    </ul>
  </div>
</body>

</html>

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

I attempted to separate a string containing <br> tags using the explode function, but the method was not successful

In my code, there is a string that looks like this <br> ACCEPT:YES <br> SMMD:tv240245ce <br> This string is stored in a variable called $_session['result'] My task is to extract the following information either as an array or ...

Encountering difficulties linking to a stylesheet or a script in an HTML file delivered by Express server

Currently, I'm facing the challenge of breaking down my Express app code into multiple files. Unfortunately, I am unable to utilize <link href> or <script src> for linking stylesheets or scripts. Below is the relevant snippet from my inde ...

Is there a way to detect the presence of a @keyframes rule without having to loop through all the

Looking for a method to determine if a CSS3 animation with a specific name is present, without the need to loop through all CSS rules. Any solution using either a JS library or plain JS will suffice. ...

Is there a way to extract all the text following a certain element using BeautifulSoup?

Here is some of the HTML code I am working with: <p> As for Madame O., if she has found employment since June 2016 that allows her to earn a monthly net salary of 1,780.50 euros (document 38 of the respondent)... </p> <div class="faits ...

What is the best way to design a form with two dropdown menus, where the options in the second dropdown menu are determined by the selection made in the first dropdown menu

Is it possible to create two select menus using one dictionary in Django, where the values in the second menu are dependent on the key selected in the first menu? In my Django views.py file, I've constructed a dictionary with Projects as keys and cor ...

Determine whether the current page was reached by pressing the back button

Can we determine if the current page was loaded via a back button press? Here is the scenario: index.html (contains a link to page1 in the menu) page1.html (loads content from ajax with a link to page2) page2.html (user presses the BACK button) page1.h ...

Initiate a fade-in/fade-out slider when the button is clicked

I have successfully developed a fadein/fadeout slider that automatically transitions between images. However, I am now looking to implement the functionality to manually play the slider by clicking on next/prev buttons. HTML <section class="wrapper"&g ...

Finding the Solution: Unraveling an Image String with Python

I need help decoding an image string. For example, I have a string that represents the Google logo. How can I determine the encoding used and decode it correctly? Example of google logo : (Here you will find instructions on styling properties followed by ...

Embed the Facebook Share Button using iFrames

I currently have a website that showcases news articles in a list format using PHP and MySQL. You can check it out here: My goal is to incorporate a Facebook share button/link on each article so that users can easily share individual posts on their own Fa ...

Concealing HTML pages in Node.js: A step-by-step guide

I have a specific requirement where my website's html pages should only be accessible to users who are logged in. However, when I used the command below, my html pages became public: app.use(express.static('public')); For instance, I do no ...

Angular - Dividing Values within Input Arrays

In the input field available to users, they can enter multiple inputs separated by commas. <div class="container"> Enter your values:<input type="text" multiple #inputCheck> <input type="submit"(cli ...

Is it feasible to share HTML5 media captures and display them in real-time on another page within the website?

Recently, I just learned about the html5 media capture API and I am excited to start experimenting with it. One thing that caught my attention is capturing the camera on the same page, but I haven't come across any information about streaming through ...

Turn off scrolling but allow dragging on iPhone

Is it possible to disable scrolling but still allow dragging on the <canvas> element in iPhone? Currently, when you drag the <canvas>, the entire page also scrolls, which is not the desired behavior. <meta name="viewport" content="wid ...

Implementing Dynamic CSS Styles in AngularJS

Essentially, I am facing a challenge on my page where a control needs to toggle two different elements with two distinct CSS classes upon being clicked. While I have successfully managed to toggle one of the controls, the other remains unchanged. Here is ...

CSS border margin-bottom attribute not functioning correctly

I am trying to create a page border by wrapping it around the content using a <div> element. The parent element is the actual page itself. However, I'm having trouble with the margin-bottom property as it doesn't seem to be working properly ...

URL not functioning properly on Django CMS menu

After setting up django-cms and creating a collapsible menu with categories and subcategories, I encountered an issue. When clicking on a main category, the URL appears correct but it does not navigate to the corresponding page. Main categories without chi ...

What is the best way to incorporate quotes within inline CSS code?

Can anyone help me with inserting the following CSS into HTML? Here is the CSS snippet that needs to be converted to HTML: .group .circular-progress::before { ... content: "10%"; ... } This is what I have attempted so far: <div class="group"&g ...

Ways to repair the mouse hover transform scale effect (animation included)

I am currently facing an issue with my GridView that contains images. When I hover over the top of the image, it displays correctly, but when I move to the bottom, it does not show up. After some investigation, I suspect that there may be an overlay being ...

Using JQuery to apply a CSS class or check for a CSS class on multiple button elements that

Being new to Jquery and Javascript, I am facing challenges in understanding how to implement the following. Throughout my site, I use the same button class, and I am attempting to make Jquery apply a "clicked" class only to the button that was clicked, no ...

The process of downloading a webpage onto a computer is not completely successful

I recently created a webpage on WIX and attempted to download it using CTRL+S. However, when I loaded the site from my computer, certain elements were not functioning properly. Is there a specific piece of code that is missing when saving the webpage in th ...