The hover CSS effect in the dropdown menu does not apply to sibling elements, but does work on descendant elements

Below are two sets of code for creating a dropdown menu. Although similar, the codes have a slight difference. In both cases, I have assigned classes to the main list item and submenu. The main heading has been given the 'heading' class with an anchor element in each of its elements. When I apply ":hover" on the anchor element combined with the submenu (dropdown element), the code does not function properly. However, if I apply ":hover" on the class 'heading' instead, the drop-down works. I will now share the code to provide more clarity. The following code successfully creates the drop-down menu by applying hover on the 'heading' class. I have included comments in the CSS code to indicate which part of the code is being referenced.

.menu{
padding: 20px;
background: #d80000;
}
.mainmenu{
display: flex;
list-style: none;
}
.heading{
margin-right: 1px;
}
.mainmenu .heading a{
display: inline-block;
padding: 10px;
text-decoration: none;
background: #fff;
color: #d80000;
width: 80px;
text-align: center;
position: relative;
}

.submenu{
list-style: none;
margin-left: -40px;
display: none;
position: absolute;
}

.submenu a{
border-top: 3px solid #d80000;
width: 80px;
}

.heading a:hover{
background: #d80000;
color: #fff;
}

/* Here the hover and submenu element works to make the element display as block*/
.heading:hover .submenu{
display: block;
}
<html>
<head> 
<title>Menu</title>
<link rel="stylesheet" type="text/css" href="style5.css">
</head>
<body>
<div class="menu">
<ul class="mainmenu">
<li class="heading"><a href="#">Home</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">About</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Services</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Products</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Contact</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>

I will now present the code that is not functioning correctly for the dropdown menu. In this case, the anchor element is applied with the hover effect along with the submenu class.

.menu{
padding: 20px;
background: #d80000;
}
.mainmenu{
display: flex;
list-style: none;
}
.heading{
margin-right: 1px;
}
.mainmenu .heading a{
display: inline-block;
padding: 10px;
text-decoration: none;
background: #fff;
color: #d80000;
width: 80px;
text-align: center;
position: relative;
}

.submenu{
list-style: none;
margin-left: -40px;
display: none;
position: absolute;
}

.submenu a{
border-top: 3px solid #d80000;
width: 80px;
}

.heading a:hover{
background: #d80000;
color: #fff;
}

/* Here, the hover on the anchor element and submenu class does not work to display the element as block */
.heading a:hover .submenu{
display: block;
}
<html>
<head> 
<title>Menu</title>
<link rel="stylesheet" type="text/css" href="style5.css">
</head>
<body>
<div class="menu">
<ul class="mainmenu">
<li class="heading"><a href="#">Home</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">About</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Services</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Products</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
<li class="heading"><a href="#">Contact</a>
<ul class="submenu">
<li class="items"><a href="#">A</a></li>
<li class="items"><a href="#">B</a></li>
<li class="items"><a href="#">C</a></li>
<li class="items"><a href="#">D</a></li>
<li class="items"><a href="#">E</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>

The question arises as to why the first code sample works for creating the dropdown menu while the second code does not. The second code applies hover on the anchor element, whereas the first code utilizes hover on the 'heading' class of the main menu.

Answer №1

 .header a:hover .subnav
                 ^

This is an example of a descendant combinator.

<li class="header"><a href="#">About</a>
     <ul class="subnav">

In this case, the .subnav is a sibling of the a element, not a descendant.

To target this relationship, you would need to use one of the sibling combinators.

Answer №2

Ensure to include the a element in your CSS selector for the second scenario:

.heading a:hover .submenu{
    display: block;
}

The error lies in this section of your code:

.heading:hover .submenu{
    display: block;
}

Answer №3

Utilize the general sibling selector to target .submenu elements that are siblings of .heading a:hover. For example:

.heading a:hover ~ .submenu{
    display: block;
}

However, if you move your cursor down to the submenu itself, you will no longer be hovering over the a element, causing the submenu to hide with display:none;. To fix this, ensure that the submenus themselves stay visible by setting display:block; when hovered over. Example:

.heading .submenu:hover {
    display: block;
}

See the working demo below:

... (content unchanged for brevity)

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

Firefox's handling of collapsing margins

Summary: Visit this codepen for an example that works smoothly on Chrome, but shows misalignment in Firefox. I have been working on a custom jQuery plugin that enhances a text input by adding a dropdown button on the left side. To ensure proper positionin ...

execute numerous Jssor Caption instances simultaneously

I have a Jssor slider and I want to create an animation for the first slide. My goal is to make two images come from the sides and merge in the center. I tried using a div for each image with a caption, and it works well. However, the second image starts ...

Implementing a function as the `data-*` attribute for an element in Angular 6

I have a question about my component.ts file: import { Component, OnInit, AfterViewInit, ElementRef, Inject } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser'; import { environment } from '../../../enviro ...

Using Angular to Bind Checkbox Value in Typescript

I have a challenge of creating a quiz where a card is displayed with 4 questions structured like this: <div class="col-md-6"> <div class="option" id="Answer1"> <label class="Answer1"> <input value= "Answer1" type="checkbox ...

Leveraging HTML within jQuery UI autocomplete

In the past, I was able to include HTML in the JSON array I used for autocomplete with jQuery UI before version 1.8.4. For example: $row_array['label'] = '<span style="color: red; font-family: courier;">User, Name</span>'; ...

IE11 adds additional spacing around list elements

I am encountering a peculiar issue with how Internet Explorer 11 handles unordered lists, unlike other browsers. Below is a comparison of how the ul appears in IE11: And here is how it looks in Chrome and other browsers: .iconImgBA { max-width: 2.4r ...

Creating consistency in tab spacing within a text area

At the moment, I am utilizing an HTML textarea attribute to collect input from users and then displaying that text back to them formatted. However, there seems to be an issue with how it handles tabs. For instance, when attempting to input the following: ...

Executing functions and setting element values upon loading in Javascript

I am currently working on updating a small Javascript function that assigns an active class to the parent <div> of a group of radio buttons when the page loads and also when there's a change event. The existing function looks like this: functi ...

AngularJS search box functionality allows users to easily search through a

1 I am looking to develop a search box using AngularJS similar to the one shown in the image. While I am familiar with creating a normal text box, I am unsure of how to incorporate the search icon. Any guidance on how to achieve this would be greatly appr ...

Drop down menu alignment issue: unable to center menu on the page

My dropdown menu is misaligned and not centering on the page, even after multiple attempts to fix it using HTML and CSS only. I have refrained from using JavaScript or JQuery for this issue. CSS Code: <style type="text/css"> /* STYLING FOR DROPDOWN ...

When the CSS style sheet is not embedded within the HTML document, it fails

I'm facing an issue while trying to apply currency formatting to an HTML table in order to have it display correctly when opened in Excel. Initially, I tried doing this inline and it worked fine, like so: <td style="mso-number-format:$\##&bso ...

Enable the ability for anchor links to be clickable when the CSS media print format is used to generate

To illustrate: <a href="https://www.google.com">Google anchor link</a> on the website, it displays as: Google anchor link When printed, it shows as: Google anchor link(https://www.google.com) To address this issue, I included in the CSS: ...

What is the best way to align text and images for list items on the same line?

HTML:- ul { list-style-image: url(https://i.sstatic.net/yGSp1.png); } <ul> <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, consequatur?</li> <li>Lorem ipsum dolor sit amet, consectetur adipisici ...

How to extract JSON data from an HTTP request in Java

I'm seeking to scrape data from a specific link using a Java program. The initial page is not an issue, but when attempting to gather data from subsequent pages, the source code remains the same as on the first page. The vital information I require is ...

Guide to placing a button on the same line as text with the use of JavaScript

Does anyone know how to add a button to the right of text that was added using JS DOM? I've tried multiple techniques but can't seem to get it right - the button always ends up on the next line. Any tips on how to achieve this? var text = docu ...

Tips for aligning a cluster of floating buttons at the center in Vuetify:

This is the code I am currently working with: <v-container height="0"> <v-row align="center" justify="center"> <v-hover v-slot:default="{ hover }" v-for="(option, index) in options" ...

Cursor starts to move to the front of the input line after every single letter with a 1 millisecond delay while browsing certain websites, such as the comments section on Youtube

When I type a letter in the field, within 1 millisecond the cursor jumps to the beginning of the line in the typing field, causing text to be typed in reverse. I can only send messages on certain sites, such as YouTube comments and Yandex Translate, for ex ...

Is there a way to enable hover functionality on mobile devices? I wish for the hover effect seen on desktop to be triggered automatically on mobile devices

I attempted to implement @media (hover: hover) without success. In my design, I have three images in a row that reveal a text overlay when hovered over with a mouse. However, the issue arises when I try to switch to a mobile view, as the images remain un ...

What is the best way to display long text in a browser alert without it being cut off?

Seeking to display a large amount of data in an alert box, but only a portion is currently visible. The following text is all that can be seen: ["19467,1496257152583","19227,1496256651094","19469,1496257033968","17285, ...

Styling in sass gets even more powerful when using custom selectors that are

Using these mixins makes it easy to work with BEM syntax in sass 3.3.2: =b($name) .#{$name} @content =e($name) &__#{$name} @content =m($name) &--#{$name} @content +b(menu) +e(item) color: grey +e(item) +m(alert) ...