Tips for customizing the appearance of li elements when hovering without changing the styles of their

Currently, I am tackling the task of customizing a sidebar menu using CSS. This menu structure involves nested <ul> and <li> elements. The issue at hand is that when sub-menu items are hovered over, their parent elements also become activated.

My understanding is that a style like

#menu-sidebar-main>li:hover>a
inhibits the cascading effect beyond the first <li><a> element within the top-level <ul>, designated as #menu-sidebar-main. Is there a similar technique to prevent lower-level <li> elements from affecting higher-level elements?

In the examples provided below, you can observe that hovering over a MID-LEVEL element triggers highlighting of its parent TOP-LEVEL element as well. Similarly, when hovering over a BOTTOM-LEVEL element, both its parent MID-LEVEL and TOP-LEVEL elements are highlighted.

Take a look at the code snippet and JS Fiddle link for further reference:

/* CUSTOM STYLING FOR SIDEBAR MENU */

/* Top Level Styles */

#menu-sidebar-main {
  padding: 0px;
  list-style-type: none;
  text-transform: uppercase;
}

#menu-sidebar-main li {
  background-color: #797979;
}

#menu-sidebar-main li a {
  padding-left: 10px;
  color: #000000;
  text-decoration: none;
}

#menu-sidebar-main>li:hover,
#menu-sidebar-main>li:hover>a {
  background-color: #791416;
  color: #ffffff;
}

/* Mid-Level Styles */

#menu-sidebar-main li ul {
  padding: 0px;
  list-style-type: none;
}

#menu-sidebar-main li ul li {
  background-color: #b3b2b2;
}

#menu-sidebar-main li ul li a {
  padding-left: 10px;
  color: #000000;
  text-decoration: none;
}

#menu-sidebar-main>li>ul.sub-menu>li:hover,
#menu-sidebar-main>li>ul.sub-menu>li:hover>a {
  background-color: #791416;
  color: #ffffff;
}

/* Bottom-Level Styles */

#menu-sidebar-main li ul li ul {
  padding: 0px;
  list-style-type: none;
}

#menu-sidebar-main li ul li ul li {
  background-color: #edebeb;
}

#menu-sidebar-main li ul li ul li a {
  padding-left: 20px;
  color: #000000;
  text-decoration: none;
}

#menu-sidebar-main>li>ul.sub-menu>li>ul.sub-menu>li:hover,
#menu-sidebar-main>li>ul.sub-menu>li>ul.sub-menu>li:hover>a {
  background-color: #791416;
  color: #ffffff;
}

/* Current Page Highlighting */

li.current_page_item {
  background-color: #791416 !important;
}

li.current_page_item a {
  color: #ffffff;
}
<!DOCTYPE html>
<html lang="en-us">

  <head>
    <meta charset="UTF-8>
    <title>Test</title>
  </head>

  <body>
    <ul id="menu-sidebar-main" class="menu">
      <li id="menu-item-268" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-71 current_page_item menu-item-has-children menu-item-268"><a href="#">Top Level Active Page</a>
        <ul class="sub-menu">
          <li id="menu-item-269" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-269"><a href="#">Mid Level</a></li>
          <li id="menu-item-270" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-270"><a href="#">Mid Level</a></li>
          <li id="menu-item-271" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-271"><a href="#">Mid Level</a></li>
          <li id="menu-item-272" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-272"><a href="#">Mid Level</a>
            <ul class="sub-menu">
              <li id="menu-item-273" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-273"><a href="#">Bottom Level</a></li>
              <li id="menu-item-274" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-274"><a href="#">>Bottom Level</a></li>
              <li id="menu-item-275" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-275"><a href="#">>Bottom Level</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </body>

</html>

Answer №1

Make sure to apply the changes specifically to the a element that is being hovered over, rather than the entire li element and its submenus:

/* STYLING FOR SIDEBAR MENU */

/* Top level styling */
#menu-sidebar-main {
  padding: 0px;
  list-style-type: none;
  font-family: "Fjalla One";
  text-transform: uppercase;
}

#menu-sidebar-main li {
  background-color: #797979;
}

#menu-sidebar-main li a {
  padding-left: 10px;
  color: #000000;
  /* temporarily set to #000000 for testing purposes */
  text-decoration: none;
  display: block; /* Added to achieve requested effect */
}


#menu-sidebar-main>li:hover>a {
  background-color: #791416;
  color: #ffffff;
}

/* Mid level styling */
#menu-sidebar-main li ul {
  padding: 0px;
  list-style-type: none;
}

#menu-sidebar-main li ul li {
  background-color: #b3b2b2;
}

#menu-sidebar-main li ul li a {
  padding-left: 10px;
  color: #000000;
  text-decoration: none;
}


#menu-sidebar-main>li>ul.sub-menu>li>a:hover {
  background-color: #791416;
  color: #ffffff;
}

/* Bottom level styling */
#menu-sidebar-main li ul li ul {
  padding: 0px;
  list-style-type: none;
}

#menu-sidebar-main li ul li ul li {
  background-color: #edebeb;
}

#menu-sidebar-main li ul li ul li a {
  padding-left: 20px;
  color: #000000;
  text-decoration: none;
}


#menu-sidebar-main>li>ul.sub-menu>li>ul.sub-menu>li>a:hover {
  background-color: #791416;
  color: #ffffff;
}

/* Current page styling */
li.current_page_item {
  background-color: #791416 !important;
}

li.current_page_item a {
  color: #ffffff;
}

UPDATE: In addition, included display: block on a elements to fulfill the desired functionality.

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

unable to modify the content within a div by clicking on a link

Lately, I've been experimenting with a code snippet I found on this fiddle: http://jsfiddle.net/unbornink/LUKGt/. The goal is to change the content of a div when clicking on links to see if it will work on my website. However, no matter which link I c ...

What steps should I take to ensure that my Material Design Icon Fonts are appearing correctly on the screen?

I can't seem to get my icons to display properly. I have included the path like this: <link href="material/vendors/material-icons/material-design-iconic-font.min.css" rel="stylesheet"> This is what I currently see: But thi ...

`ASP.NET utilized for displaying several pictures on a website`

My goal is to retrieve images from a database and showcase them on a webpage. I am looking to incorporate a "show more images" button in case there are too many images to display at once. Additionally, I would like for the additional images to load autom ...

Using a combination of CSS selector, several attributes, and the OR operator

Can you assist me with CSS selectors? Let's say we have a style HTML tag: <style type="text/css"> [...selector...] {color:red;} </style> I want to create a selector with multiple attributes and logical operators, but I am uncertain about ...

How can I implement a hover effect without triggering a click event?

My structure is pretty basic, here it is: .container { height: 100vh; width: 100%; pointer-events: none; } .click-layer { background-color: #ccc; position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: auto; } .box { ...

Why isn't the background of the container div at the top when using multiple CSS sheets?

I am revitalizing a dull website and seeking feedback on my template design here: Check out the ideal look It's quite lovely! However, as I attempt to incorporate my background, menu, and footer elements, I seem to be experiencing issues with my con ...

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

Is it possible to create a dynamic <hr /> using Flexbox?

In the midst of working on my application, I managed to achieve a simple layout. However, there is one peculiar requirement that has been eluding me for the past 2 days and now I find myself in need of assistance. Here is the current HTML code I am workin ...

numbered navigation jquery plugin

Can anyone recommend a jQuery plugin or alternative solution for creating a navigation system for comments similar to YouTube? Here is the link for reference: Thank you in advance. ...

I'm puzzled as to why the hidden input field consistently returns the same value every time

As a beginner in php, I am facing an issue where I cannot trace the correct value of the hidden input which represents the ID of the image in the products table. Every time I try to delete an image, it always returns the ID of the last image in the product ...

JavaScript Event Listener not Working Properly

I've been attempting to implement a feature on my website where the header hides when scrolling down and re-appears when scrolling up. However, I'm struggling to make use of the code snippet below to achieve this functionality. Despite my thoroug ...

Is it possible to overlay a vertical middle on top of an image with an undefined height?

I've been struggling to make this work for the last 24 hours. Initially, I got it working in Chrome using CSS tricks' block:before method, but then realized it didn't function properly in Firefox. I attempted the table method as an alternat ...

Encountering an issue with npm installation following a recent node version upgrade

Having trouble installing Sass on Node version v16.14.0. I keep receiving this error message: https://i.stack.imgur.com/6KNcF.png ...

Enhance your images with a hover zoom effect while viewing an overlay on top

Is there a way to make an image still zoom in on hover even when it has an overlay using CSS? I am currently working with Bootstrap 5.2 Thank you in advance. .trip-card { position: relative; } .trip-card img { border-radius: 30px; } .overlay { ...

Searching for text within an HTML document using Selenium in Python can be easily achieved by using the appropriate methods and

Is there a way to find and retrieve specific texts within an HTML file using Selenium in Python, especially if the text is not enclosed within an element? <div class="datagrid row"> ==$0 <h2 class="bottom-border block">Accepted Shipment</h ...

The div has extra white space at the bottom due to the Hide/Show content feature

Having trouble stretching a scrolling div to 100% of its parent container's height? The Hide/Show content feature is causing whitespace at the bottom of the div. Check out the jsfiddle here: http://jsfiddle.net/fkvftff2/1/ LATEST UPDATE: The issue a ...

What are the steps to creating a gradient for the bottom border?

SAMPLE TEXT: Hi there, I am looking for assistance in designing a bottom border with a gradient and transparent edges. Any suggestions on how I can achieve this effect? ...

The rendering of CSS is altered when being processed by IIS

It appears that this issue is not specific to any browser and generally affects styling in various webkit/gecko/mozilla styles. For instance, when I create a website mockup and implement a style like: element.class { border-radius: 5px; -moz-bor ...

Adjusting the characteristics of a class when hovering

Is it possible to change the CSS property of a class when hovering over another class in CSS? #_nav li { display:inline; text-decoration:none; padding-top:5vh; line-height:8vh; margin-right:5vh; cursor:pointer; } #_nav li:hover + ...

I am struggling with aligning the list items side by side

I am currently facing an issue with my list items that have anchor tags. They are currently set to display block with background images, but I want them to display inline. However, they are stacking on top of each other instead. The ul is being generated b ...