Having trouble making :hover work properly

I've been attempting to create a mega menu that activates divs on hover, but I just can't seem to get it to work.

I've tried various methods, but the divs simply won't show when hovered over.

If you'd like to take a look, here's the basic concept in jsfiddle: http://jsfiddle.net/mXx64/5/

Below is the code:

HTML

<div class="nav-container">
    <ul id="mega-menu">
        <li class="menu1">
            <a href="#">menu1</a>
        </li>
        <li class="menu2">
            <a href="#">menu2</a>
        </li>
        <li class="menu3">
            <a href="#">menu3</a>
        </li>
        <li class="menu4">
            <a href="#">menu4</a>
        </li>
    </ul>
</div>

<div class="menu-area1">
    menu1
</div>

CSS

.nav-container ul li a:hover .menu-area1 {
    display: block;
}

.menu-area1 {
    display: none;
    height: 100px;
    width: 100px;
    background-color: red;
}

Any assistance would be greatly appreciated!

edit Please disregard the spelling error, I'm aware of it, but fixing it doesn't resolve the issue with the code :(

Answer №1

Your code will not function properly as the element with the class ".menu-area1" is not within the anchor tag. To maintain this structure, you must incorporate jQuery. Alternatively, you can follow this revised approach for it to work correctly: http://jsfiddle.net/8uK3Q/

<div class="navigation-container">
  <ul id="mega-menu">
    <li class="item1"> <a href="#">Item 1</a>
      <div class="menu-section1"> Item 1 Section </div>
    </li>
    <li class="item2"> <a href="#">Item 2</a> </li>
    <li class="item3"> <a href="#">Item 3</a> </li>
    <li class="item4"> <a href="#">Item 4</a> </li>
  </ul>
</div>

.menu-section1 {
    display: none;
    height: 100px;
    width: 100px;
    background-color: red;
}
.navigation-container ul>li:hover .menu-section1 {
    display: block;
}

Answer №2

I may not be an expert in JQuery, but here is the code snippet I managed to put together:

Check out the Fiddle

$('li').hover(function () {
    $('.menu-area1').show();
});

$('li').hover('out',function () {
    $('.menu-area1').hide();
});

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

When using Nav, it is necessary to only have one dropdown open at a time by closing others before opening a new

I am encountering an issue with my dropdown menu system. I have multiple submenus within a top-level dropdown menu, and the problem is that when I open one submenu and then click to open another, the first one remains open. I would like for the other subme ...

Attempting to utilize Flexbox to populate vacant areas

https://i.stack.imgur.com/BhPU0.png Can the yellow square smoothly transition to the empty grey square and beyond in this layout setup? Each flex item represents a component that can utilize either 50% or 100% of the container width, information only know ...

Is it possible to create dynamic animations with SVG files?

I am currently in the process of coding my website, and I came up with an exciting idea to animate my navigation bar within an SVG container. With its naturally wavy design, I aim to achieve a sweeping wave effect similar to the intro animation on Discord. ...

My CSS horizontal drop-down menu is causing the sub-navigation items to overlap each other

I'm having an issue where my sub navigation menu items are stacking on top of each other instead of displaying properly. Here is the code snippet causing the problem: /* STYLING FOR NAVIGATION MENU */ .nav-bar { width: 100%; height: 80px; ...

Is there a way in CSS to set a label's width equal to the width of its content?

I am facing an issue where I need the question mark div to be positioned right next to the top row of text in the label it is associated with. Can someone suggest some styles that can be applied to the label to adjust the width and spacing so that it match ...

Modify the width of the <nz-date-picker> component from Ng-Zorro

Currently using Ng-Zorro for my template styling and working on implementing a date picker that I want to align perfectly with the dropdown menu above it. I would like to adjust the width of the date-picker manually within the template, but after visiting ...

Please refer to the appropriate manual for your MySQL server version to find the correct syntax for the following statement: `'' , , '', '', '', '', '', '', NOW())' at line 2`

I encountered an error and since I am new to programming, I would appreciate any corrections. Thank you! <? $customername = $_REQUEST["customername"]; //$customername = strtoupper($customername); //$customername = trim($customername); $address1=$_RE ...

Ways to hide menu click when hovering over it

As a beginner, I have created a menu that shows options on hover and click. However, I am encountering an issue where clicking on one menu option and then hovering over another menu creates two lists of options. Is there a way to make the clicked menu opti ...

Placing the ul tag within a div at the top of the div for proper alignment

I am facing an issue with my code where I cannot get the UL element to appear at the top of the div as it is a child element. <!doctype html> <html> <head> <title>xxx</title> </head> <body> <div id="page" style ...

Trouble getting vertical alignment to work using display:table-cell within display:table

This is my initial inquiry, so I apologize if the formatting is incorrect. Despite attempting various methods from Stack Overflow, I am still unable to properly display this content vertically centered. CSS: div.img-wrapper { display: table; height: ...

Issues with CodeIgniter paths causing disruption to CSS background-image declaration

I've been working on a website with backend for two separate customers, each with their own unique URLs. I encountered an issue with Javascript links (ajax calls using url:) but managed to resolve it by using a global variable: var SiteURL='< ...

A guide on retrieving the present directory name and showcasing it on an HTML page with the help of Node.js

At the moment, I am able to view the path name in the index.js file and see the current directory name in the node.js console when I select the directory. Now, I am looking to have that directory name displayed in the text box on the template.html page. C ...

Is there a way to view the text as I enter it while drawing text on an image canvas?

I currently have a canvas setup that allows users to upload an image and display it on the canvas. Users can then input text to be drawn on the image by clicking the submit button. However, I would like the entered text to appear on the image as it is bein ...

Using the jQuery method `fadeToggle` to handle the functionality of multiple buttons toggling hidden text

I am struggling to make fadeToggle work properly. I want to reveal text when clicking on another div. Since I plan to use this for multiple elements, I am looking for a smart and efficient way to handle them all. Here is my current attempt which does not ...

Enhancing Drag and Drop Functionality with jQuery and CSS Zoom Effect

I am facing an issue in my application with a div that has variable CSS zoom. Due to this, the coordinate space gets disrupted. The page pixel is no longer equal to 1px when hovering over the zoomable area :) This breakdown in the coordinate system is ca ...

What is preventing me from assigning all padding values at once?

I attempted to modify all paddings of the html element using JavaScript, with the following code: const pd = 1 document.getElementsByClassName('board')[0].style.paddingRight = pd + 'px' document.getElementsByClassName('board') ...

Adjusting scroll position delay for a fixed element when using an Android device

I am experiencing an issue with an element that is supposed to become fixed when scrolling past 145px and then unfixed when scrolling back up. While this functionality works smoothly on desktops, there seems to be a delay on mobile devices, particularly on ...

Ways to resolve delay in updating values in vuejs following an axios request

When fetching data from an api to populate specific <select></select> elements linked to radio buttons, I am facing an issue. The radio buttons trigger an axios get request upon an event like @change/@click, and the data is successfully retriev ...

I attempted to craft a toggle button by applying and removing an active class that I had previously designed, but unfortunately, it did not function as intended

Every time I click on a button, I keep encountering this error message. I am certain that my selector is correct, but I can't seem to figure out why I'm getting the Uncaught TypeError: Cannot read property 'classList' of undefined at HT ...

Having trouble getting a background image to show up in a table cell when using CSS clear:both?

I am working on an email and I want an image to only show up on mobile devices. To achieve this, I created an empty <td> with a span inside where I styled the span to have a background image. However, my issue is that I want the image to occupy an e ...