Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images.

First screenshot below shows that it works properly: https://i.stack.imgur.com/3Xn6T.png

Second screenshot below shows the submenu appearing when hovering over a list item in the submenu:

https://i.stack.imgur.com/de3nZ.png

Now let's review the HTML code:

<nav class="header-navigation">
    <ul>
        <li><a href="">Home</a></li>
        <li>
            <a href="">Blog Styles</a>
            <ul>
                <li><a href="">Image Post</a></li>
                <li><a href="">Audio Post</a></li>
            </ul>
        </li>
        <li>
            <a href="">Level 1</a>
        </li>
    </ul>
</nav>

And here is the CSS:

nav.header-navigation ul > li > ul{
    background: #fff;
    box-shadow: 0px 2px 15px rgba(0,0,0,0.1);
    position: absolute;
    z-index: 999;
    width: auto;
    top: 21px;
    white-space: nowrap;
    padding: 15px 17px 13px;
    margin-top: 18px;
    left: 0;
    visibility: hidden;
    opacity: 0;
    transform: translateY(10px);
    -webkit-transform: translateY(10px);
    -moz-transform: translateY(10px);
    -ms-transform: translateY(10px);
    -o-transform: translateY(10px);
    transition: all .3s ease;
    -moz-transition: all .3s ease;
    -webkit-transition: all .3s ease;
    -o-transition: all .3s ease;
    -ms-transition: all .3s ease;

}
nav.header-navigation > ul > li:hover > ul{
    visibility: visible;
    opacity: 1;
    transform: rotate(0deg) translateY(0px);
    -webkit-transform: rotate(0deg) translateY(0px);
    -moz-transform: rotate(0deg) translateY(0px);
    -ms-transform: rotate(0deg) translateY(0px);
    -o-transform: rotate(0deg) translateY(0px);
}
nav.header-navigation > ul > li > ul > li:hover > ul{
    visibility: visible;
    opacity: 1;
    transform: rotate(0deg) translateY(0px);
    -webkit-transform: rotate(0deg) translateY(0px);
    -moz-transform: rotate(0deg) translateY(0px);
    -ms-transform: rotate(0deg) translateY(0px);
    -o-transform: rotate(0deg) translateY(0px);
}

Answer №1

If my understanding is correct, you are encountering difficulties when hovering outside the link labeled "Blog Styles" with the same y-coordinate but different x-coordinate. This issue arises because the current width of the "li" element covers your entire screen, allowing for hover interactions at any x-coordinate as long as the y-coordinate remains constant.

To resolve this problem, adjust the width of the "li" element to specify a smaller area for hovering. For instance -

    <ul>
    <li><a href="">Home</a></li>
    <li class = "test">
        <a href="">Blog Styles</a>
        <ul>
            <li><a href="">Image Post</a></li>
            <li><a href="">audio post</a></li>
        </ul>
    </li>
    <li>
        <a href="">Level 1</a>
    </li>
</ul>

By applying the same CSS rules and introducing the class ".test" with a specific width -

.test {
   width: 100px;
}

The class will now have a width of 100 pixels, restricting hover actions within the designated list area only.

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

How to trigger a function when clicking on a TableRow in React using MaterialUI

Can someone help me understand how to add an onClick listener to my TableRow in React? I noticed that simply giving an onClick prop like this seemed to work: <TableRow onClick = {()=> console.log("clicked")}> <TableCell> Content </Ta ...

Changing the value of one particular key within an object during a reduce operation will impact all other keys within the object as well

I'm completely lost here. It seems like there's a reference issue causing all data properties to be overwritten with the value of the last row. I need help figuring out how to iterate over a list of objects, using specific keys to assign data to ...

What is the best way to delay an observable from triggering the next event?

In my Angular project, I am implementing RxJs with two subjects. s1.next() s1.subscribe(() => { // perform some operation and then trigger the event for s2 s2.next() }); s2.subscribe(() => { // perform some operat ...

I am interested in updating the color of the active item on the navbar

I am looking to modify the color of the active page, Here is the HTML code snippet: <html> <head> <title>John Doe - Portfolio</title> <link rel="stylesheet" href="custom-style.css"> <link rel=" ...

Only one admin account can be accessed at a time by multiple logins

I am looking to add a new feature to my app where only one admin can log in at a time. If someone else tries to log in with the same admin ID on another device, a warning should be shown, indicating that the user is already logged in and cannot access the ...

Dynamically add a plugin to jQuery during execution

After installing jQuery and a jQuery-Plugin via npm, I am facing the challenge of using it within an ES6 module. The issue arises from the fact that the plugin documentation only provides instructions for a global installation through the script tag, which ...

dimming of dojo dijit.Dialog

I've been encountering issues with "undimming" a Dojo Dijit dialog. Even though I am using the appropriate calls, there are instances where parts of the page remain dimmed. I can still scroll through the windows and access the undimmed sections. Is th ...

Issue with resetting input forms in ReactJS

I have a simple form that seems to be clearing the rest of the fields every time I try to fill it. Additionally, validation errors are being displayed below each field instead of just one. How can this issue be fixed? Here is how the form looks before any ...

Flex sidebar collapse menu

I am currently in the process of developing an admin panel for my website. I have a sidebar menu that I would like to hide behind a hamburger icon and only expand when clicked on. After researching potential solutions, I haven't found much that fits m ...

The function User.find does not exist and it is not possible to replace the `users` model after it has

Currently, I am experimenting with using mongoose, mongoDB, next, and express in a test project. Despite referencing solutions like Cannot overwrite model once compiled Mongoose and others, I am encountering issues unique to my situation. Upon initializat ...

Adjust the appearance of a glyphicon by modifying its color according to various criteria

Using angularjs, I implemented ng-repeat to display details in a table. In the final column, I aim to display the glyphicon 'glyphicon glyphicon-stop' in a specific color. <tr ng-repeat="ps in $ctrl.personLst"> <td>{{ ps.id}}</td& ...

Exploring the Diversity of Forms with Ajax and JQuery

$(document).ready(function () { $("#addrow").click(function () { var newRow = '<tr><td><input type="text" class="item_code form-control" placeholder="Item Code" name="item_code[]"></td><td><input type="text" ...

Create a circle at the point where two table cells intersect in an HTML document

How can I create a circular shape at the intersections of HTML tables? I am struggling to figure out a way to incorporate shapes into HTML tables. My goal is to achieve a design similar to the image shown below. I have attempted using text and spans, as we ...

Enhancing navigation functionality using CSS

I have two separate pages with navigation included in both. This way, it's easier to edit the navigation menu once instead of doing so on each page individually. However, I am now facing uncertainty on how to implement the 'active' class usi ...

Tips for converting the Instagram cURL post request to a JavaScript request

I am attempting to convert the code I received from Instagram into a token. The code provided in the Instagram DOCS uses a curl request, but I would like to implement it using JavaScript instead. Here is how the original code looks: curl -X POST &bsol ...

AngularJS: How service query data is perceived as an object and therefore cannot be utilized by Angular

When retrieving data from my PHP server page in the factory service, I encountered two different scenarios: If I call a function that returns an array and then use json_encode($data); at the end, Angular throws a resource misconfiguration error due to ...

Is it possible to align divs so that they touch when they wrap to a new line, creating a grid-like appearance?

My React board component consists of an array of divs that I want to arrange in a grid-like map. The issue is, when the div wraps to a new line, there is significant space between each row. I aim to have the divs close together with no gaps. GameMap state ...

Ways to achieve perfect alignment for SVG text within its container, as opposed to stretching across the entire webpage

Recently, I've been working on a customizable photo frame designer using SVG. One of the key features allows users to add their own text to the frame. To achieve this, I'm utilizing jQuery's .keyup function. However, the issue I'm facin ...

There seems to be a syntax error in the AngularJS/Bootstrap code, with an unrecognized expression

Hey there, I'm currently working on developing an application using Angular and Bootstrap. I've successfully implemented ui.router for routing purposes, but I've encountered an issue when loading the Bootstrap library. The console is showing ...

Transition/transform/translate3d in CSS3 may lead to significant flickering on the initial or final "frame" of the transition (specifically on an iPad)

Hello everyone, I am currently developing a web application specifically for the iPad and I have implemented a CSS3 transition to animate a div, moving it from left to right. Here is the structure of my class: .mover { -webkit-transition:all 0.4s ea ...