Element disappears upon hovering over hyperlink

I have set up a side menu on my website. I am trying to make it so that when the user hovers over the Manage Contracts button, a sub menu will appear. To achieve this, I have created two ul elements - one for the primary menu and one for the sub menu. The reason for separating them is to prevent the sub menu from inheriting styles from the primary menu. However, I am facing an issue with getting the sub menu to appear when the mouse hovers over the Manage Contract button. Any suggestions?

Here is the HTML code:

<div id="navbar-side">

    <div id="profile-image">

    </div>
    <div id="menu-search">
        <input type="text" name="search" id="search" placeholder="Search..." />
    </div>
    <ul id="side-menu">
        <li><a href="#" title="Dashboard">Dashboard</a></li>
        <li><a href="#" title="Manage Contracts" id="contracts">Manage Contracts</a></li>
        <li><a href="#" title="Manage Duplicates">Manage Duplicates</a></li>
        <li><a href="#" title="PPM Manager">PPM Manager</a></li>
        <li><a href="#" title="Service User Search">Service User Search</a></li>
        <li><a href="#" title="My Subscriptions">My Subscriptions</a></li>
        <li><a href="#" title="Help Centre">Help Centre</a></li>
    </ul>

    <ul id="contracts-menu">
        <li><a href="#">General</a></li>
        <li><a href="#">Settings</a></li>
        <li><a href="#">Communication</a></li>
        <li><a href="#">Contract Details</a></li>
        <li><a href="#">Retail Setup</a></li>
    </ul>
</div>

And here is the CSS code:

#side-menu 
{
width:100%;
position:relative;
}

#side-menu li 
{
display:block;
border-bottom:solid 1px black;
}

#side-menu a 
{
display:block;
border-top:solid 1px white;
padding:18px 0;
}

#side-menu a:hover 
{
background-color:Silver;
}

#contracts-menu 
{
display:none;
width:200px;
height:300px;
background-color:Aqua;
margin-left:228px;
position:absolute;
top:87px;
}

#contracts-menu li 
{
display:block;
}

#contracts-menu a 
{
display:block;
}

a#contracts:hover + #navbar-side ul#contracts-menu
{
display:block;
}

Answer №1

That's because a#contracts and div#navbar-side are not direct siblings!

You cannot use the + or ~ selectors on elements that are not siblings.

Unfortunately, CSS does not currently have a parent selector.

To work around this issue, you can utilize JavaScript or modify your HTML structure as shown below:

<li><a href="#" title="Manage Contracts" id="contracts">Manage Contracts</a>
    <ul id="contracts-menu">
        <li><a href="#">General</a></li>
        <li><a href="#">Settings</a></li>
        <li><a href="#">Communication</a></li>
        <li><a href="#">Contract Details</a></li>
        <li><a href="#">Retail Setup</a></li>
    </ul> 
 </li>

CSS:

a#contracts:hover + ul#contracts-menu {
    display:block;
}

Check out the JSBin Demo.

Update

In this scenario, it is unnecessary to assign an id attribute to each link/item.

You can nest the sub-menus within each list item and reveal them with the following CSS:

#side-menu li:hover > ul {  /* Select the ul children inside each list-item */
    display:block;
}

See JSBin Demo #2 for more details

Answer №2

A workaround for avoiding the use of javascript is to nest the ul#contracts-menu inside the list-item that includes the #contracts anchor:

<div id="navbar-side">

    <div id="profile-image">

    </div>
    <div id="menu-search">
        <input type="text" name="search" id="search" placeholder="Search..." />
    </div>
    <ul id="side-menu">
        <li><a href="#" title="Dashboard">Dashboard</a></li>
        <li id="contracts">
            <a href="#" title="Manage Contracts">Manage Contracts</a>
            <ul id="contracts-menu">
                <li><a href="#">General</a></li>
                <li><a href="#">Settings</a></li>
                <li><a href="#">Communication</a></li>
                <li><a href="#">Contract Details</a></li>
                <li><a href="#">Retail Setup</a></li>
            </ul>
        </li>
        <li><a href="#" title="Manage Duplicates">Manage Duplicates</a></li>
        <li><a href="#" title="PPM Manager">PPM Manager</a></li>
        <li><a href="#" title="Service User Search">Service User Search</a></li>
        <li><a href="#" title="My Subscriptions">My Subscriptions</a></li>
        <li><a href="#" title="Help Centre">Help Centre</a></li>
    </ul>
</div>

Add the id to the list item (instead of the anchor) and then include this CSS style:

#side-menu li.contracts:hover #contracts-menu { display:block; }

View it in action here: http://jsfiddle.net/FmR3f/

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

The main menu is not displaying on mobile devices when in portrait orientation

Currently delving into the world of CSS/HTML for mobile devices, I've encountered an issue with my site. It looks great on desktop, but on a mobile device, the menu disappears. However, when switched to landscape mode, the menu magically reappears. An ...

The .css() method does not apply any styles to the elements in the document

Having trouble with a jQuery function: .css() This is the HTML code in question: <div class="..." id="about"> // OTHER HTML CODE </div> And here's the corresponding jQuery code: (function($, window, document) { $(function() { ...

The CSS property object-fit: cover is unable to properly display JPEG images with EXIF orientation greater than 1

I'm having trouble with my Angular app and creating a gallery of photos from my Samsung Galaxy. I am using the "object-fit: cover" css attribute for a nice design, but it only seems to work correctly when the image has an EXIF "orientation" property e ...

Leveraging the power of PHP sessions within jQuery/HTML/Ajax

Following a successful login attempt, my PHP script initializes a session in the following manner: session_set_cookie_params(1800,'/','www.mydomain.com',true); session_start(); header("location:mainpage.html"); Now, I have ...

Troubles with CSS grid design

Could someone assist me with my grid layout for a project on free code camp? I am trying to display 3 of my portfolio pages in a row using a grid layout, but I'm having trouble getting the middle page to align properly with the others. Since I am new ...

How can JavaScript be used to automatically send a user to a designated page based on a selected option in

I am in the process of creating a JavaScript function that redirects users based on their selection from a dropdown menu. Additionally, I need to ensure that the word "admin" is set for both the username and password fields. view image here function my ...

Can someone explain how to use JavaScript to make table data fill the entire row in a table?

After clicking the button, the layout of the table changes. I want to keep the original table layout even after the JavaScript function runs. I am still learning about JavaScript. function toggle(button) { if(document.getElementById("1").value=="Show ...

How come the font size and div elements combine when I drag and drop the items?

Recently, I decided to create my own drag and drop game. The game is almost complete, but there's one issue. When I try to drop the items into the designated "Drop Items Here" area, their style changes abruptly to mimic the text. For example: https: ...

Using jQuery to toggle the visibility of div elements while displaying an indicator

A query arises regarding the utilization of JQuery to facilitate toggling the visibility of text divs. The question pertains to how one can incorporate an indicator - such as an up and down arrow graphic - to denote whether the divs are open or closed. Sug ...

Captivating captions for captivating visuals. Pictures don't conform to a linear arrangement

As someone who is new to website creation, I am currently working on adding a CSS effect where a popup-text appears when an image is hovered over. I want to create a row of images for the popups by using an unordered list. However, I'm facing a proble ...

Choose each day's time slot on the jQuery FullCalendar version 2.x

The code snippet below is a segment of the code generated by the FullCalendar jQuery plugin version 2. It has undergone some changes from version 1.x in terms of the classes it utilizes. <div class="fc-slats"> <table> <tbody> ...

What could be the reason for the lack of response from the jquery element on an

Recently, I've been working on a jQuery project with sticky headers. However, when I tested it on my iPad, I noticed a slight delay and the header jumping around the page. I'm wondering if this issue can be resolved within the code itself or if ...

Having trouble accessing a section of my website that uses JQuery Mobile and tags

I'm encountering a small issue and I'm not sure how to resolve it. The problem I'm facing is with a HTML5 code containing different pages (tags with ids). I'm utilizing Jquery Mobile to switch between these pages using buttons. Strangel ...

Change the color of child li to white when hovering over the parent li, and vice versa

Let me start off by explaining the issue I'm facing. Take a look at to get a better understanding of what I'm referring to. It needs to be a specific page, not the home page, so you can see the problem for yourself. Now, when you hover over the ...

Ways to show dropdown menu link exclusively on mobile browsers and not on desktop browsers

<li class="megamenu-content"> <div class="megamenu-content-wrapper clearfix"> <ul class="col-lg-3 col-sm-3 col-md-3 "> <li class="cat-header">DISPLAY MEMBERS</li> ...

What safety measures should be implemented when granting users permission to modify HTML and CSS on your site?

I'm really impressed by the customization options on Tumblr. Users can edit the HTML and CSS of their profiles, which is something I'd love to incorporate into my own site. However, I'm concerned about the security implications of allowing t ...

Can a media source have a buffer that doesn't kick off with a key frame?

Is it feasible to utilize the media source extension to implement buffering such that buffers initiate with a typical frame rather than a key frame? For example, could the video be buffered frame by frame? (Even though that may not be the most practical a ...

Unable to activate dropdown menu in html and css

Hi there, I'm facing an issue with hiding the dropdown-menu submenu. I tried using display: block !important;, but it didn't work. Even when I used display: none !important;, the dropdown menu still wouldn't disappear. I want the dropdown s ...

Attempting to determine which PHP form was submitted

I'm currently working on creating multiple forms within the same page, but I've encountered an issue where the PHP code is not executing properly when attempting to submit the form. <form id="DeleteEmployeeModal" name="DeleteEmployeeModal ...

Whenever I run my HTML through a server, it doesn't seem to connect with my CSS file

I'm currently developing a Django app, and I have encountered an issue where the CSS is not loading when I run different pages through the server. Oddly enough, everything works fine when I open the files directly without using the server. The problem ...