When I hover over my button, my dropdown unexpectedly vanishes

Does anyone know how to prevent the dropdown from disappearing when hovering on the button? I'm not using JavaScript for this dropdown menu and would appreciate any help. I'm still learning, so please bear with me as I try to resolve this issue.

   <nav>
        <div class="dropdown">
          <a><img class="profile" src="profile.png" href="index.html"/></a>
          <button><a href="#" class="home">Nieuws</a></button>
          <div class="projects">
            <button>Koers<i class="fas fa-chevron-down"></i></button>
            <ul>
              <li><a href="nieuws.html">Weather App</a></li>
              <li><a  class="menulink"  href="#">Music App</a></li>
              <li><a href="#">React App</a></li>
              <li><a href="#">Youtube App</a></li>
            </ul>
          </div>
          <div class="products">
            <button>Kopen/Verkopen<i class="fas fa-chevron-down"></i></button>
            <ul>
              <li><a href="#">Cloud Service</a></li>
              <li><a href="#">Music Streaming</a></li>
              <li><a href="#">Consulting Help</a></li>
            </ul>
          </div>
          <div class="Contact">
            <li><a href="contact.html">Contact</a></li>
          </div>
        </div>
      </nav>

CSS:

 * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  
  body {
    height: 100vh;
    background-color: rgb(139, 62, 85);
    font-family: "Poppins", sans-serif;
  }
  a,
  button {
    font-family: "Poppins", sans-serif;
    color:white;
    text-decoration: none;
  }


.profile {
    width: 110px;
    height: 110px;
    background-color: hotpink;
    margin-left: -10px;
   
}
.header{ 
    background-color: hotpink;
    margin-left: -10px;
    margin-right: -50px;
    margin-top: -10px;
    margin-bottom: 150px;
  
}
.Contact{
    list-style:none;
    color:white;
    text-decoration: none;
}
  .dropdown {
    width: 60%;
    margin: auto;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  
  .projects,
  .products {
    position: relative;
  }
  
  .projects ul,
  .products ul {
    position: absolute;
    display: flex;
    justify-content: space-around;
    flex-direction: column;
    align-items: center;
    width: 200px;
    height: 200px;
    background: white;
    left: 0px;
    list-style: none;
    border-radius: 5px;
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
    transition: all 0.4s ease;

  }
  .projects li,
  .products li {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    color:white;
  }
  
  .projects li:hover,
  .products li:hover {
    background-color: rgb(197, 173, 181);
   
   
  }
 
  .projects a,
  .products a {
    color: black;
    text-decoration: none;
  }
 
  
  .dropdown button,
  .home {
    background: none;
    text-decoration: none;
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    
  }
  
  .projects button:hover + ul ,
  .products button:hover + ul{
    opacity: 1;
    pointer-events: all;
    transform: translateY(0px);
    
  }
.projects button ul{
  display: block;
}



  i{
    margin-left: 10px;
    align-items: center;
  }

Thanks!

Answer №1

To achieve the desired effect, it is recommended to insert the :hover pseudo element into the parent container rather than the button itself.

 * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  
  body {
    height: 100vh;
    background-color: rgb(139, 62, 85);
    font-family: "Poppins", sans-serif;
  }
  a,
  button {
    font-family: "Poppins", sans-serif;
    color:white;
    text-decoration: none;
  }


.profile {
    width: 110px;
    height: 110px;
    background-color: hotpink;
    margin-left: -10px;
   
}
.header{ 
    background-color: hotpink;
    margin-left: -10px;
    margin-right: -50px;
    margin-top: -10px;
    margin-bottom: 150px;
  
}
.Contact{
    list-style:none;
    color:white;
    text-decoration: none;
}
  .dropdown {
    width: 60%;
    margin: auto;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  
  .projects,
  .products {
    position: relative;
  }
  
  .projects ul,
  .products ul {
    position: absolute;
    display: flex;
    justify-content: space-around;
    flex-direction: column;
    align-items: center;
    width: 200px;
    height: 200px;
    background: white;
    left: 0px;
    list-style: none;
    border-radius: 5px;
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
    transition: all 0.4s ease;

  }
  .projects li,
  .products li {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    color:white;
  }
  
  .projects li:hover,
  .products li:hover {
    background-color: rgb(197, 173, 181);
   
   
  }
 
  .projects a,
  .products a {
    color: black;
    text-decoration: none;
  }
 
  
  .dropdown button,
  .home {
    background: none;
    text-decoration: none;
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    
  }
  
  .projects:hover ul ,
  .products:hover ul{
    opacity: 1;
    pointer-events: all;
    transform: translateY(0px);
    
  }
.projects button ul{
  display: block;
}



  i{
    margin-left: 10px;
    align-items: center;
  }
<nav>
        <div class="dropdown">
          <a><img class="profile" src="profile.png" href="index.html"/></a>
          <button><a href="#" class="home">Nieuws</a></button>
          <div class="projects">
            <button>Koers<i class="fas fa-chevron-down"></i></button>
            <ul>
              <li><a href="nieuws.html">Weather App</a></li>
              <li><a  class="menulink"  href="#">Music App</a></li>
              <li><a href="#">React App</a></li>
              <li><a href="#">Youtube App</a></li>
            </ul>
          </div>
          <div class="products">
            <button>Kopen/Verkopen<i class="fas fa-chevron-down"></i></button>
            <ul>
              <li><a href="#">Cloud Service</a></li>
              <li><a href="#">Music Streaming</a></li>
              <li><a href="#">Consulting Help</a></li>
            </ul>
          </div>
          <div class="Contact">
            <li><a href="contact.html">Contact</a></li>
          </div>
        </div>
      </nav>

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

Can you help with showing a value in a div upon clicking a button?

I am relatively new to this. I am trying to have a value displayed in a div that has already been defined in CSS when a button is clicked. However, all that appears is the button itself. I just can't seem to get the desired result. Here is what I have ...

It is recommended that the page does not scroll while utilizing both the sidenav and toolbar simultaneously in Angular Material

Using md-sidenav without md-toolbar works fine. The sidenav opens correctly, as shown in the image below. https://i.sstatic.net/PjsPp.png However, when a toolbar is added before the sidenav or its containing section, opening the sidenav causes the page t ...

When an anchor tag is embedded within a string response, ReactJS fails to render it as a clickable link

I am in the process of transitioning our code from Freemarker to React JS and I am encountering an issue with rendering anchor tags from a JSON string response. When I display this string in my React JS application, the anchor tags are appearing as plain t ...

I am experiencing issues with the required tag on my email input in the form. Despite using the built-in HTML5 required function, it seems to be malfunctioning. Any suggestions on how to resolve this?

Currently tackling one of the 5 projects in my freecodecamp Responsive Web Design course (the survey project) and facing an issue with utilizing the built-in required function in HTML5 for my email input field. Despite my best efforts, upon hitting enter, ...

Ensuring that a paragraph text wraps neatly inside a div container

I recently came across a post discussing the issue of text wrapping in IE 9 here. My main goal is to have my text wrap only once in this particular browser. The content consists of just two lines, and I need it to smoothly transition from the first line to ...

Is it a Mozilla Firefox glitch or something else?

After writing the code, I noticed a bug in Firefox and a small bug in Chrome. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

The process of displaying video information on web browsers by streaming platforms

https://i.sstatic.net/XgnsX.png I recently noticed a feature in Chrome where there is a tab next to the extensions that shows videos playing in the browser. This tab only appears when a song or video is being played. For example, when I start a video on Y ...

What are some effective strategies for preventing CSS duplication?

What is the best way to prevent repeating CSS code like this: div#logo div:nth-child(1) div { width: 30%; height: 30%; background-color: white; } div#logo div:nth-child(3) div { width: 30%; height: 30%; background-color: white; ...

Issues encountered with utilizing a Python dictionary within a Django template

Hey there, I've got a python dictionary that holds information about size and quantity titled final_list. I'm passing this dictionary to the template using "sizes": final_list. In my html template, I'm trying to create a dropdown m ...

access an external link without disrupting automatic meta refresh

On my simple Teamspeak viewer site, I display the current users online. To auto-refresh the page every 60 seconds, I use this code: <meta http-equiv="refresh" content="60;url=index.html"> There is a button on the site that allows users to connect ...

Is there a way to load the right amount of images in advance, depending on the size of the

I'm currently trying to optimize the loading of Largest Contentful Paint (LCP) candidate images based on device size, but I'm encountering difficulties. The issue at hand: On smaller screens, only one image is displayed in a carousel On larger ...

Behavior of floating footer in Wordpress across different web browsers is inconsistent

I'm facing an issue with the footer of my WordPress website, where it behaves differently in Chrome and Firefox. In Chrome, the right part of the footer seems to be moving out of alignment with the left part, while it displays correctly in Firefox. Y ...

Incorporate a PowerPoint presentation into an Iframe

Can you confirm if this code is correct? Is there another way to achieve the same result? Could you please provide some assistance with the code? I would like to show my PowerPoint file in an iframe when I click on a link. <html> <head> < ...

What is the best way to start tiny-slider automatically once the video has ended?

I am currently using the tns-slider plugin and have a setup with 3 slides (2 photos and 1 video). <div class='tiny-slider'> <div class='slide slide1'> <div class='video-slide'> <video id=&qu ...

Opting for HTML tags directly rather than using div.class

On my blogsite, I have implemented special tags that are designed to be user-friendly for my colleagues who may not be familiar with HTML. For instance: <question>...</question> and <answer>...</answer> These tags are then style ...

Creating an interactive R Shiny application with custom CSS styling and a modal

I am encountering an issue when trying to incorporate a modal dialog with a specific CSS style within a R Shiny app. Individually, I can successfully implement a modal dialog without the CSS style and apply the CSS style without any issues. However, when a ...

Develop a container element with protective measures against external CSS styles impacting internal elements

As I work on integrating my webpage code into a large project, I have encountered an issue. The entire project code is contained within a div element inside the <main> tag. However, when I add this container div within the <main> tag, the pro ...

Preventing image overlap in Bootstrap when window size is reduced

Here is the code snippet: <div id="members"> <div class="container-fluid"> <div class="row"> <div id="members" class="col-md-12 text-center"> <h3>Text</h3> &l ...

Anchor tags created using JQuery will remain on the same page without redirecting

Presented below is the code I have utilized to construct the anchor tag along with its content. $('div#right-slide').html("<a href=\"http://www.XXXXXXXXXXXX.info/limited-specials/\" ><h1 id=\"specials\">Click Here ...

The height of my row decreases when I implement the z-index for a hover effect

Hey there! I'm currently working on creating a hover effect for my cards using Bootstrap and z-index. However, I've run into an issue where the z-index works fine when I hover over the cards, but the row loses its height. I tried adding a height ...