Highlight the active link in the parent node changes color when the child node is in focus

I've designed a CSS vertical menu with the code provided below. You can view the test page at .

The issue I'm facing is that when hovering over the sub-menu items, the highlighted 'li' in the parent node reverts back to the non-hover color (#297BCE). I would like it to remain white (#FFF) as it does when hovered over. I've been struggling to identify what I might be missing. Any suggestions on what I could be overlooking?

<style>
#categories {
 background-color:#297BCE;
 width:214px;
 height:37px;
  -moz-border-radius-topright: 8px;
  border-top-right-radius: 8px;
  -moz-border-radius-topleft: 8px;
  border-top-left-radius: 8px;
  position:absolute;
  top:67px;
  padding-top:10px;
  z-index:3;
  font: bold 15px Arial, sans-serif; color:#FFFFFF;
 }
#navigation {  position:absolute;font-size:0.75em; width:214px;top:115px;}
#navigation ul {margin:0px; padding:0px;}
#navigation li {list-style: none;} 
/* FIRST LEVEL TEXT/BACKGROUND/BORDER SETTINGS */
ul.top-level {background:#fff;}
ul.top-level li {
 font-family: arial, sans-serif;
 font-size:14px;
 font-weight:bold;
 border:1px solid #D2D2D2; border-color:#fff #A5A5A5 #A5A5A5 #A5A5A5;
background:#fff url(images/arrow.gif) no-repeat center right;
}
/* FIRST LEVEL TEXT COLOR */
#navigation a {
 display:block;
 color: #297BCE;
 cursor: pointer;
 line-height: 25px;
 text-indent: 10px;
 text-decoration:none;
 width:214px;
}
/* FIRST LEVEL TEXT HOVER COLOR */
#navigation a:hover{
 color: #FFF;
 text-decoration:none;
}
/* FIRST LEVEL HOVER BACKGROUND COLOR */
#navigation li:hover {
 background-color:#297BCE;
 position: relative;
}
#navigation li{
 background-color:#FFF;
 position: relative;
}
/* SECOND LEVEL TEXT SETTINGS */
ul.sub-level li a{
    font-family: arial, sans-serif;
    font-size:12px;
    font-weight:normal;
    background-color:#FFF;
}
ul.sub-level {
    display: none;
}
li:hover .sub-level {
    position: absolute;
    display: block;
    background: #FFF;
    border: #A5A5A5 solid;
    border-width: 1px;
/* set the sub menu flyout position here */
    left: 207px;
    top: -15px;
    z-index:10;
}
/* SECOND LEVEL TEXT HOVER COLOR */
#navigation ul li ul li a:hover{
 color:#f90;
 background-color:#FFF;
 text-decoration:underline;
}
#navigation ul ul{
    padding-top:5px;
    padding-bottom:5px;
    border-radius: 8px;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
     box-shadow: 0px 3px 10px #B0B0B0;
}
#navigation ul li ul li a{
 text-indent: 20px;
 line-height: 17px;
}
ul.sub-level li {
    background:#fff;
    border:1px solid #D2D2D2;
    border-color:#FFF #FFF #FFF #FFF;
    float:left;
}
</style>

<div id="categories">View All Categories</div>
<div id="navigation">
    <ul class="top-level">
        <li><a href="#">Home</a>
            <ul class="sub-level">
                <li><a href="#">Sub Menu Item 1</a></li>
                <li><a href="#">Sub Menu Item 2</a></li>
                <li><a href="#">Sub Menu Item 3</a></li>
                <li><a href="#">Sub Menu Item 3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">FAQ</a>
            <ul class="sub-level">
                <li><a href="#">Sub Menu Item 1</a></li>
                <li><a href="#">Sub Menu Item 3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">News</a>
            <ul class="sub-level">
                <li><a href="#">Sub Menu Item 1</a></li>
                <li><a href="#">Sub Menu Item 2</a></li>
                <li><a href="#">Sub Menu Item 3</a></li>
            </ul>
        </li>
    </ul>
</div>
</div>  

Answer №1

To ensure that only the immediate child link changes to white when hovering over the 1st level li, utilize the child selector syntax (>) in your CSS.

Include this code in your CSS:

#navigation li:hover > a{
    color:#fff;
}

Check out: http://jsfiddle.net/MVKLC/
for a live demonstration.

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

Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what&ap ...

What is the reason behind div elements shifting when hovering over a particular element?

Currently, I have floated all my div elements (icons) to the left and margin-lefted them to create space in between. I've displayed them inline as well. However, when I hover over one element (icon), the rest of the elements move. Can you please help ...

How to utilize Bootstrap 4's d-flex to make an element span the entirety of the available space?

Creating this specific design is now achievable with the new bootstrap d-flex feature. Can you guide me on how to craft a section similar to the one depicted in the second image? https://i.sstatic.net/LtTux.png https://i.sstatic.net/RlWvJ.png ...

What is the best way to customize the color of the icon in the <v-text-field>?

I have a <v-toolbar> component and I am trying to customize it by adding a search text field with a search icon in front: <v-text-field ...

ChessboardJs: JavaScript Boards Function Properly on Initial Use Only

Update: The page code can be accessed via my page URL. I'm unsure where the issue lies. Your assistance is appreciated. Issue: Initially, when clicking on the chess puzzles page, everything works fine. However, upon re-clicking from the homepage, th ...

Exploring the relationship between CSS z-index values and a canvas

http://jsfiddle.net/y2q3yLpj/ In my example, I tried setting the z-index of a canvas to 0 and the z-index of a div to 1. However, despite this configuration, the canvas appeared higher than the div. Surprisingly, when I set the z-index of the canvas to -1 ...

Styling a div element in CSS so that it adjusts its size to fit its

How can I make a div element adjust to its content and also break the line so that the next element appears below it? I attempted setting the display property to inline-block, but then the div no longer breaks the line... Normally, it breaks the line, but ...

Create a sleek and uniform design with Bootstrap by setting up three columns that maintain the same height

I am attempting to design a 3 column div that maintains equal height for all responsive rows, each containing an ICON and information. <div class="container"> <div class="row row-eq-height"> <div class="col c ...

Incorporate CSS and JavaScript files into every page of NetSuite

Is there a way to globally apply a CSS file or JavaScript code to all NetSuite pages in order to change the page direction to RTL? I attempted adding it through: SuiteScript >> Client >> Deployment : All Records, While I was able to successfu ...

Add a css class to a <div> that is created by an <ng-template> inside the <ngx-datatable-column> element

I am struggling to implement the display: flex style on the parent <div> containing my <span> However, since the <span> is dynamically generated by the ng-template of the ngx-datatable-column, I'm finding it challenging to apply thi ...

Transforming the navigation menu using CSS, HTML, and jQuery

One challenge I am facing involves creating a menu similar to the one on http://edition.cnn.com/. I want the clicked button in the menu to receive focus, while the others lose it. Despite trying various methods, I have not been successful. Can someone off ...

Using HTML video to fill entire div container

I'm currently facing an issue with using an HTML video as a fullscreen background replacement for an image. The problem is that the video overflows and covers the entire page instead of staying within the constraints of the header section. Here is th ...

Tips for incorporating a responsive width feature into the Drawer component in MUI v5

I'm currently working with Material UI v5 and facing a challenge in creating a responsive drawer. I want the drawer to occupy 100% of the screen width on smaller devices, while taking up only 1/3 of the screen width on larger devices. Unfortunately, I ...

Looking for assistance with aligning UL elements in CSS using absolute positioning for a dropdown effect

I'm currently working on implementing a language switching feature on this website. The concept involves using a SPAN element that reveals a dropdown box (based on UL) containing a list of available languages when hovered over. Below is a preview of t ...

Tips for designing a navbar specific to a profile section

I am currently working on an angular application with a navigation bar composed of anchor tags. When the user logs in, I have an ngIf directive to display my profile icon with dropdown options. However, I am facing challenges in styling it correctly. I aim ...

Place a vertical slider adjacent to an HTML element on either the left or right side

I'm struggling to bind a range slider to the left or right side of a canvas that displays video. Despite my efforts, CSS seems to be putting up a strong fight. I can handle issues like stretching and slider values, but attaching it to the canvas is pr ...

Tips for customizing CSS for the ValidatorCallout Extender in an ajax tool

I am currently attempting to implement the ValidatorCallout Extender feature from the asp.net Ajax website. However, I am struggling with customizing the CSS for the popup validator. ...

What could be the reason for the body background color refusing to change

I'm currently working on a JavaScript calculator project and I'm using this code snippet from codepen. The issue I'm facing is that despite setting the body background color to black, it still appears white. Could there be a spacing problem ...

Aligning the content in the center of a div with inline display

Hi there, I have a quick question for the experts out there. I'm new to all of this so please bear with me :) I'm attempting to center an inline social sharing div on my website, but it's proving to be quite challenging. The div is nested w ...

Challenges with the final child element's CSS styling

Hey everyone, I've recently added the following CSS: #tab-navigation ul li:last-child { background-image: url(../images/tabs-end.gif); background-repeat: no-repeat; color: #fff; display: block; border: 1px solid red; backgrou ...