What causes the dropdown to appear when hovering over the main div, but not when hovering over the button?

During my attempts to display a dropdown menu when the user hovers over a button, I encountered an issue where the dropdown only appeared when I added the hover action to the parent container div. When attempting to change the .dropdown:hover selector to .dropdownbtn:hover (the fourth selector that changes the display to block), it did not work, potentially causing problems in practical scenarios. I am wondering why this is happening – could it be related to the positioning of elements?

If you'd like to see the code in action, check out the fiddle here.

* {
  font-family: sans-serif;
  font-weight: bold;
  margin: unset;
  text-decoration: none;
}

.dropdownbtn {
  border: 0ch;
  min-width: 50px;
  min-height: 50px;
  background-color: rgb(58, 199, 58);
}

.dropdown:hover .dropdownbtn {
  background-color: rgb(44, 151, 44);
}

.dropdown:hover ul.menu {
  display: block;
}

.dropdown {
  position: relative;
  display: inline-block;
  margin: 10px;
  text-align: center;
}

.dropdown-content {
  position: absolute
}

ul {
  list-style-type: none;
  margin: 0px;
  padding: 0px;
}

ul>li {
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu {
  display: none;
  box-shadow: 10px 0px 95px 0.1px rgba(0, 0, 0, 0.472);
}

.menu>li {
  text-decoration: none;
  min-width: 105px;
  min-height: 50px;
  padding: 10px;
  background-color: rgb(58, 199, 180);
  border-bottom: black 1px;
}

.menu>li:hover,
.sub-menu>li:hover {
  background-color: rgb(47, 148, 134);
}

.sub-menu-toggle {
  position: relative;
}

.sub-menu {
  position: absolute;
  display: none;
  left: 125px;
  top: 0px;
  background-color: rgb(58, 199, 180);
  box-shadow: 10px 0px 95px 0.1px rgba(0, 0, 0, 0.472);
}

.sub-menu>li {
  padding: 10px;
  min-height: 50px;
  min-width: 105px;
}

.sub-menu-toggle:hover .sub-menu {
  display: block;
}

.dropdown-content li::before {
  content: ;
}
<div class="dropdown">
  <button class="dropdownbtn">Dropdown button</button>

  <div class="dropdown-content">
    <ul class="menu">
      <li><a href='#'>Home</a></li>
      <li class="sub-menu-toggle">
        <a href='#'>Products</a>
        <ul class="sub-menu">
          <li><a href='#'>Product 1</a></li>
          <li><a href='#'>Product 2</a></li>
          <li><a href='#'>Product 3</a></li>
          <li><a href='#'>Product 4</a></li>
        </ul>
      </li>
      <li><a href='#'>Contact</a></li>
    </ul>
  </div>
</div>

Answer №1

The issue seems to be related to the positioning of elements. It was noted that .dropdown:hover is functioning correctly, but modifying it as shown below does not yield the desired result:

.dropdownbtn:hover ul.menu {   
    display: block;
 }

The reason for this discrepancy is that the selector specified here is targeting a descendant element with the class menu within the .dropdownbtn, which is non-existent.

By using .dropdown:hover, the style is applied because the ul.menu element is a child of the div.dropdown element, rather than being nested inside the button.dropdownbtn element.

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

What steps can be taken to identify the script responsible for preventing text highlighting or right clicking on a webpage?

Whenever I visit aliexpress using Chrome, I encounter issues where I am unable to highlight text on an item's page (although it works fine on the search results list) and cannot right-click to access the context menu on images. Both of these functions ...

Obtain the database username, password, database name, and host information from the user in order to update a specific column in the database

I am a newcomer here and inexperienced in programming. I have developed a script that can modify a database column, but now I aim to acquire the database login information from users. Once users provide all required info correctly, the script will alter th ...

Conceal the div elements when the variable $[value] is devoid of content

I'm working on creating some divs that represent boxes, but I only want the box to show if a value is filled out. How can I accomplish this? Below are the divs I have: <div id="age" class="leifbox" style="float:left;height:20px;text-align:center; ...

Having trouble accessing the EJS image with express.static in Node JS

i am attempting to retrieve an image from the uploads path using EJS, here is my file route here is the code in 'dotemplate.js' <div class="col-xs-12"> <h2 style="text-align: center; font-size: 12pt" >ATTACHMENT</h2> & ...

Supporting Custom Tags in Java EE for Enhanced Functionality

Consider the following scenario in a Java EE setting: <input name="foo" value="hello" /> If we use ServletRequest.getParameterNames(), we would anticipate seeing foo. However, if we have something like this instead: <footag name="foo" value="h ...

Expanding Items' Widths in ItemsControl on Silverlight

I am facing an issue with my ItemsControl where the child items are not occupying the whole width of the control: My goal is to stretch the green bits to fill the width of the control, similar to how the blue bits are shown. I have attempted adjusting th ...

Utilize jQuery to show a specific section of the page based on the hash in the URL

So, the inspiration for this project stemmed from a common issue I encountered: After hitting the register button, the PHP script processes it and displays an error, but the page remains on the login form. Here's the visual representation of the i ...

Achieving consistent hover effects in both Firefox and Chrome with CSS

Currently, I am faced with a dilemma regarding some CSS hover effects and table formatting. Despite successfully implementing most aspects of the design, there is an issue with how different browsers interpret padding within elements during color changes o ...

Library for creating animated effects on HTML5 canvas elements in mobile phone applications using Javascript

Which JavaScript Animation Library is recommended for optimal performance on mobile devices using the HTML5 Canvas Tag for game development? I have found that many of the currently available libraries do not meet the necessary performance requirements ...

What is the method for moving one div above or below another div using the scroll bar?

Here's the scenario I'm facing: I have rows with buttons that, when clicked, reveal a set of options. The challenge is that depending on where the row is located on the page, the settings need to open either above or below the button. When the b ...

What is the best way to create more room for my button arrangement?

--How can I adjust the spacing of the buttons?--The animations are working, but I need to create some padding between them-- I want all buttons to stay on the same line but with a little more space between each one--I'm still relatively new to this, s ...

Repetitive outcomes are observed when iterating through elements with Selenium in Python

I'm currently facing a puzzling issue that has me stumped. My HTML contains a ul list with multiple li items, each of which includes elements I need to extract using Selenium in Python. The structure of the website is as follows: <ul id="results"& ...

Problem with JQuery Dialog Box and Div

Within my HTML I have a div labeled "passage-content". This div's text is meant to be displayed in a jQuery dialog box when a button is clicked, however the content of the div is constantly changing due to the dynamic nature of the page. Each time the ...

Activate Bootstrap Modal with a Click of a Button

I'm interested in customizing a Bootstrap 4 modal, similar to the example on this page: https://www.w3schools.com/bootstrap4/bootstrap_modal.asp. My goal is to trigger the modal to open when clicking on specific text instead of a button. I believe us ...

Demystifying Vertical Alignment: Everything You Need to Know

Struggling with vertical alignments has proven to be more complicated than expected. Despite reading numerous resources on stackexchange, a common understanding of the process remains elusive. After gathering some guidelines, it turns out that vertical-al ...

Conceal a nested button depending on the value of an input field

I am facing a challenge with hiding a button inside a form. <form method="get"> <input type="hidden" name="id" value="7245"> <input type="hidden" name="action" ...

The jQuery slideToggle() function causes motion

One of my web elements utilizes slideToggle to display/hide a table. This functionality is achieved using jQuery $("#featMoreInfo").click(function() { $("#featben2").slideToggle('slow'); var txt = $(this).text() == '+ More Info ...

How can I style an empty input text field with CSS?

Imagine a scenario where a form is initially loaded with an <input type="text"> that has a vibrant green background. What would be interesting is if, once the user starts entering a value, the color changes to blue. The big question is: Can we achie ...

Using the <br /> HTML tag may face cross-browser compatibility issues

Having some trouble with the HTML <br /> tag. I'm trying to stack 2 images on top of each other. When there is no <br /> between them, the bottom border of the top image touches the top border of the bottom image, which seems correct. Ho ...

Is there a way to adjust the brightness of specific sections within an image using html, css, and js?

I am working on a project where I have an image with tags underneath that correspond to different parts of the image. For example, if the image is of a dog, one of the tags could be 'nose', indicating the portion of the image around the dog' ...