Ways to implement varying hover color effects on distinct list items using CSS

When I created a ul inside a nav tag, I wanted to apply different background-color hover effects to different li elements. However, the hover effect is only being applied to the anchor tags and not the complete li elements.

The CSS properties that I have mentioned are as follows:

.main-menu .acc .fa-home,
.main-menu .acc>a:hover {
  background-color: #689f38;
}

.main-menu .fa-laptop,
.main-menu li>a:hover {
  background-color: #ed3b3b;
}

.main-menu .fa-list,
.main-menu .has-list>a:hover {
  background-color: #ef6c00;
}

The HTML structure with the classes for each li element is as follows:

<nav class="main-menu">
  <ul>
    <li class="acc">
      <a href="#">
        <i class="fa fa-home fa-2x"></i>
        <span class="nav-text">
           List One
        </span>
      </a>
    </li>
    <li class="has-subnav">
      <a href="#">
        <i class="fa fa-laptop fa-2x"></i>
        <span class="nav-text">
            List Two
        </span>
      </a>
    </li>
    <li class="has-list">
      <a href="#">
        <i class="fa fa-list fa-2x"></i>
        <span class="nav-text">
           List Three
        </span>
      </a>
    </li>
  </ul>
</nav>

Answer №1

Ensure that the :hover pseudo-class is applied directly to the li element, like li:hover, and not to li>a:hover

.main-menu .acc .fa-home,
.main-menu .acc:hover{
    background-color:#689f38;
}

.main-menu .fa-laptop,
.main-menu li:hover{
    background-color:#ed3b3b;
}

.main-menu .fa-list,
.main-menu .has-list:hover{
    background-color:#ef6c00;
}
 <nav class="main-menu">
  <ul>

                <li class="acc">
                    <a href="#">
                        <i class="fa fa-home fa-2x"></i>
                        <span class="nav-text">
                           List One
                        </span>
                    </a>
                </li>

                <li class="has-subnav">
                    <a href="#">
                        <i class="fa fa-laptop fa-2x"></i>
                        <span class="nav-text">
                            List Two
                        </span>
                    </a>

                </li>

                <li class="has-list">
                    <a href="#">
                        <i class="fa fa-list fa-2x"></i>
                        <span class="nav-text">
                           List Three
                        </span>
                    </a>

                </li>
                </ul>
                </nav>

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

Creating various rows within a loop can be achieved by using conditional statements to determine

I am faced with a challenge of handling an array of images, among other properties, and I aim to achieve the following outcome: https://i.sstatic.net/BYajY.png As the images reach the end of the container (which fits 4 images on desktops and adjusts for ...

What could be causing the closest() method in my JavaScript code to not locate the nearest class?

I've encountered an issue while dynamically creating new classes with input fields in my project. For some reason, when I try to remove a previous row, nothing happens. Can anyone help me troubleshoot this problem? Here is the JavaScript code that I ...

Safari experiencing a CSS issue not found in Chrome or Firefox

https://gist.github.com/2354116 When you check the link above in Chrome or Firefox, everything looks good. The divs at the bottom, including the headings and social icons, are centered within a container div without any issues. However, when viewed in Sa ...

What is the best way to ensure that text stays aligned perfectly next to an

There seems to be a problem with my CSS. The text in the <li> tag is appearing below the line of the icon. How can I resolve this issue? I want the text to be vertically centered on the line. This seems to happen when I use the ::before selector with ...

Can anyone suggest a method to set up a group chat without the need for a database?

I am interested in setting up a group chat where all messages and corresponding usernames are stored in a JSON file. However, achieving this without the use of node.js or MySQLi seems to be quite challenging. Currently, I am able to read the JSON file an ...

Adding mat-icon dynamically to divs using Angular's renderer2

I'm currently working on a project that involves a lot of HTML divs. I've already created some static divs using renderer2. static HTML (not dynamically generated) example of desired result using renderer2 <div class="time-rowss clearf ...

Determine if two JavaScript strings are anagrams of one another by comparing their characters. What specific method or approach is utilized in this scenario?

Could someone please provide a breakdown of the JavaScript code logic used here? The following code is designed to check if two strings are anagrams of each other, but I'm having trouble understanding the approach taken to perform this check. Any he ...

Placing content inside the primary div disrupts the structure of the HTML page

I have a sidebar and a navbar in my HTML file. That's all I have. My goal is to insert content into the main-content div, but when I do, it appears like this - https://i.sstatic.net/YFiFC.png I want my content to fill up the empty space. https://i. ...

Do you think there might be an issue with the CSS coding?

I have designed a user-friendly responsive login form that allows users to easily log in and reset their passwords. However, I am facing an issue where the username and password fields are not displaying on separate lines as intended. I have thoroughly ins ...

Angular encounters issue when retrieving CSS file while navigating to a nested route

When trying to access a child component directly in production mode (using ng serve --prod), it fails to load the CSS file as it attempts to fetch it from a nested path. For example, when navigating to "localhost:4200/doc/", the CSS Request URL is: loca ...

How can CSS image hover affect the layout of other elements on the page?

When I hover over the image and it grows to 350px, it causes everything around it to shift. The code is functioning as expected, but the issue arises when the enlarged image pushes nearby elements downward. Is there a way to prevent this behavior? #disp ...

Creating a gaming application with Phaser.js and Ionic with subpar rendering capabilities

Attention developers! I recently created a game app using Phaser.js. I integrated the code into an Ionic blank starter app, allowing the Ionic framework to render the view while Phaser takes care of displaying the game. Issue: The game is a simple flapp ...

CSS: positioning elements relative to the bottom right corner

The structure of the floating element is as follows: <a>The_button</a> <div style="position:absolute"> <div style="position:relative" class="inner-box"> Content Content Content Content Content ...

Is it possible to save any additions made to the DOM using JavaScript into local storage, so that it can be retrieved even after the page is reloaded?

Let's consider a scenario for testing purposes: you have a function that appends <li> elements inside an <ol> container, and you want to retain all the list items added. Is there a way to store them in Local Storage (or any other local sto ...

Unable to choose a child div using jQuery

<div class='class1'> <div class='class2'> <div class='class3'> some unique text </div> <div class='class5'> more unique text </div> </div> < ...

Images that dynamically adjust within the confines of a single div

My issue involves a simple script that includes two responsive images. When the window is minimized, the images adjust accordingly. However, when I place both images within the same div like this: <div class="wrapper"> <div class="block"&g ...

show additional worth on the console

Just starting out with JavaScript. Trying to display additional values in the console. Uncertain about how to access add-ons. Can anyone help me troubleshoot? Here is my code snippet below: https://jsfiddle.net/6f8upe80/ private sports: any = { ...

Having trouble retrieving the value from a textarea in HTML using CodeIgniter with AJAX and PHP

Having trouble fetching the value of your textarea in PHP from AJAX? It seems that when you try to retrieve the value from HTML to JavaScript using var content = $('textarea[name=post_content]').val(); console.log(content);, it displays the value ...

Is it possible for the .find() method to extract values from <a> tags?

I'm currently working on retrieving values from an anchor tag using jQuery instead of a button in my HTML. I want to style it with images because a button doesn't match the look of my website. Here's what I've been trying: var mssg_i ...

Is it possible to have the background scroll downward while keeping some content centered in place at all times?

Attempting to achieve the following effect in JavaScript, HTML, and Sass: When scrolling down the page, the layers (ground, sky, space, etc.) move downward The content (a rocket flying in the sky) stays centered on the screen and will move horizontally l ...