CSS causing navigation bar drop down content to be hidden when hovering

I'm currently working on a drop-down menu and everything seems to be functioning properly. However, when I hide my drop-down block, it doesn't reappear on hover. Can someone help me identify the issue here?

HTML-:

<div id="nav_wrapper">
   <ul>
       <li><a href="#">maths</a>
           <ul class="one">
              <li><a href="#">real number</a></li> 
           </ul>
      </li> 
      <li><a href="#">english</a></li>
      <li><a href="#">hindi</a></li>
      <li><a href="#">french</a></li>
      <li><a href="#">german</a></li>    
   </ul>
</div>

CSS-:

#nav_wrapper {
    position: relative;
    top: 20px;
    left: 100px;
}

#nav_wrapper ul {
    list-style-type: none;   
}

#nav_wrapper ul li {
    outline: 1px solid white;
    width: 160px;
    text-align: center;
    position: relative;
    float: left;
    background: black;
}

#nav_wrapper ul li a {
    text-decoration: none;
    display: inline-block;
    height: 25px;
    width: 160px;
    padding-top: 7px;
    color: #DAA520;
}

#nav_wrapper ul ul.one {
    display: none;
}

#nav_wrapper ul li a:hover {
    color: wheat; 
}

#nav_wrapper ul li a:hover > ul.one {
    display: block;
}

Currently, when hovering over MATHS, the real number drop-down block is not appearing.

Answer №1

Make sure to target the next element and not the child with this code: a:hover + ul.one

CSS selector

Explanation

  1. > is used for a child parent > child
  2. + is used for the next element firstelement + nextelemnt

Example snippet for parent> child

.child,.second{
  display:none;
  }

.first:hover > .child{
  display:block;
  }
<div  class="first">hover me
<a class="child"> child open</a>
</div>

<div class="second" > I'm a next element</div>

Example snippet for firstelement + next element

.child,.second{
  display:none;
  }

.first:hover + .second{
  display:block;
  }
<div  class="first">hover me
<a class="child"> child open</a>
</div>

<div class="second" > I'm a next element</div>

#nav_wrapper
{


    position: relative;
    top: 20px;
    left: 100px;

}
#nav_wrapper ul{
 list-style-type: none;   
 }

 #nav_wrapper ul li{
     outline: 1px solid white;
     width: 160px;
     text-align: center;
     position: relative;
     float: left;
     background: black;

 }


 #nav_wrapper ul li a{
     text-decoration: none;
     display: inline-block;
     height: 25px;
     width: 160px;
     padding-top: 7px;
     color: #DAA520;


 }

 #nav_wrapper ul ul.one
 {
     display: none;
 }
 #nav_wrapper ul li a:hover {
      color: wheat; 

 }


 #nav_wrapper ul li a:hover + ul.one{
     display: block;

 }
<div id="nav_wrapper">
                   <ul>
                       <li><a href="#">maths</a>
                           <ul class="one">
                              <li><a href="#">real number</a></li> 
                           </ul>
                       </li> 
                       <li><a href="#">english</a></li>
                       <li><a href="#">hindi</a></li>
                       <li><a href="#">french</a></li>
                       <li><a href="#">german</a></li>    
                   </ul>

           </div>

Answer №2

Hey there! I would like you to test out this code snippet: a:hover + ul.one. Feel free to check out the demo link for a live example: Demo

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

Ways to prevent users from manually inputting dates in date fields

I am currently developing an application and I need to prevent users from manually entering a date in the type=date field on the HTML page. I want to restrict users to only be able to select a date from the calendar display, which is in the format MM/DD/YY ...

Populating a read-only field with information from a database upon selecting an option

My goal is to select a name from an option field and then display the user's ID in a non-editable field. Essentially, when I choose a username, I want to automatically show their user ID. Below is the PHP and HTML code I currently have: <form id=" ...

Modifying Margin and Spacing for Selections in Radio Buttons

I have successfully implemented the code for a radio button, and everything is working as expected div > label { margin-right: 80px !important; box-shadow: .3rem .3rem .6rem #c8d0e7, -.2rem -.2rem .5rem #FFFFFF; position: relative; ...

Is there a way to retrieve the immediate children of all elements using CSS selectors in Selenium?

Although I attempted to utilize the ">" syntax, selenium does not seem to accept it. I am aware that Xpath can be used to obtain what I need, however our project exclusively utilizes CSS selectors. My goal is to create a list containing only the immedi ...

How can I ensure my game or website fits perfectly on the screen without any need

I have recently developed a fun Erase-A-Man game that is optimized for mobile devices. However, I am facing an issue where users need to scroll to view all the letters on their screens. My goal is to make the game fit perfectly on all phone screen sizes so ...

Is it possible for me to convert my .ejs file to .html in order to make it compatible with Node.js and Express?

I have an index.html file and I wanted to link it to a twitter.ejs page. Unfortunately, my attempts were unsuccessful, and now I am considering changing the extension from ejs to html. However, this approach did not work either. Do .ejs files only work wit ...

Trouble with Setting a Background Image in Ionic with Javascript and Angular

I'm having trouble getting my background image to display in this Ionic project using CSS. It works when I embed it directly into the HTML, but that's not an ideal solution. I vaguely recall something about using base64 for images, but I'm n ...

Having trouble with the "Cannot POST /contact.php" error message?

Help needed with ERROR "Cannot POST /contact.php". Here is the code: Code from index.html <form action="/contact.php" method="post"> <div class="w3-row-padding" style="margin:0 -16px 8px -16px"> <div class="w3-half"> ...

Assign a value to a variable based on the button that was clicked using a form

My form consists of 8 buttons, each representing a different color: .settings-theme-buttons{ height: 4vw; width: 4vw; border: none; display: inline-block; border-radius: 100%; cursor: pointer; } #settings-theme-white{ b ...

Repairing the Performance of the 'Save' Feature

Can the 'Save' button in my code save team assignments for players selected using drag and drop? I'm considering using localStorage, but unsure about implementation. Note: To run the code properly, copy it as an HTML file on your computer. ...

Problem with jQuery's .prepend method being called twice on list items

Looking to enhance the appearance of a list by adding some icons before the anchor links within each list item. <ul class="submenu-children"> <li><a href="#">Link</a></li> <li><a href="#">Link</a></li> ...

Tips for correctly linking JS and CSS resources in Node.js/Express

I have a JavaScript file and a stylesheet that I am trying to link in order to use a cipher website that I created. Here is my File Path: website/ (contains app.js/html files and package json) website/public/css (contains CSS files) website/public/scri ...

Tips for emphasizing the current element without disrupting existing styles

Is there a way to highlight an element with a border without causing it to shift? The script seems a bit glitchy when detecting mouse leaving the area. I'm unable to override parent element properties like border or outline. I also can't use pse ...

Angular Material: div not being filled by layout-fill

<!-- Container Div --> <div layout-fill> <!-- Image Div --> <div layout="column" layout-align="center center" class="coverImage" layout-fill> <md-content class="md-padding"> Sample Text Here ...

Tips for Maintaining the Execution of a JavaScript Function Within a Class until the Browser Window is Closed (Web Development)

The title might be a little confusing, so let me clarify here: I have implemented a popup that appears in the bottom right corner of a specific page on my website. The popup is working as intended (it shows up when the page is initially opened and can be ...

Conflicts in SwiperJS Timeline Management

Having a Timeline on my Website using SwiperJS presents challenges with conflicting functions. The goal is to navigate swiper-slides by clicking on timespans in the timeline. The desired functionality for the timeline includes: Sliding to the correspondi ...

Align the checkbox input in the center of a flexbox container

I am in the process of creating a side menu with filters, and I need to design a row that includes an input checkbox along with some accompanying text. However, I am facing an issue where the checkbox does not center properly within the flex container when ...

get direct access to the children of a specific div element

Here is the code snippet in HTML format. <div id="this"> <a href="xxx"> xx</a> <a href="yy"> yy</a> <a href="zzz"> zzz</a> aaa <a href="bbb"> bbb</a> ccc </div> I am look ...

Tips for implementing $setValidity with ng-if for the same field in Angular

Hey there, I'm currently facing an issue with using form.form_field.$setValidity on an ng-if tagged field. When testing out the code, I encountered the following error: angular.js:13642 TypeError: Cannot read property '$setValidity' of unde ...

Browsing through files on IE8 within one window

I recently encountered an issue that I have never experienced before. Typically, when browsing through my folders in Internet Explorer, everything worked smoothly for me. If I included various directory links, I could easily navigate back and forth to expl ...