Problem with the hover functionality of the <li> tag

I'm having an issue with applying the hover property to the li element in the sidebar. The hover effect works, but the icon tag is not showing the hover effect. I want the icon to also show the hover effect along with the li's hover effect. Here is the code snippet:

.wrapper {
    display: flex;
    position: relative;    
}

.wrapper .sidebar{

    position: fixed;
    width: 200px;
    background-color: #00B6E1;
    /* padding: 30px 0; */
    height: 100%;
 
}
.wrapper .sidebar ul li{


    padding: 15px;
    border-bottom: 1px solid #064E88;
    /* border-top: 1px solid; */
    /* color: white; */
    font-weight: 400;
}


.wrapper .sidebar ul li a{

    padding: 10px;
}

.wrapper .sidebar ul li:hover{

    background-color: #064E88;
    color: white;
}
<div class="wrapper">
        <div class="sidebar">
            <ul class="side">
                <li><a href="#"> <i class="bi bi-columns-gap fa-lg icon-red"></i> </a>Dashboard</li>
                <li><a href="#"> <i class="bi bi-graph-up-arrow"></i> </a>Reporting</li>
                <li><a href="#"> <i class="bi bi-send-check"></i> </a>Sending</li>
                <li><a href="#"> <i class="bi bi-envelope"></i> </a>Receiving</li>
                <li><a href="#"> <i class="bi bi-person-check"></i> </a>Verification</li>
            </ul>
        </div>
    </div>

Answer №1

You have the option to assign an additional class to each <i> tag and then reference it in CSS using the li:hover selector. (I've labeled it as icon)

<div class="wrapper">
    <div class="sidebar">
        <ul class="side">
                
            <li><a href="#"> <i class="bi bi-columns-gap fa-lg icon-red icon"></i></a>Dashboard</li> <!-- Additional class 'icon'-->
            <li><a href="#"> <i class="bi bi-graph-up-arrow icon"></i> </a>Reporting</li>
            <li><a href="#"> <i class="bi bi-send-check icon"></i> </a>Sending</li>
            <li><a href="#"> <i class="bi bi-envelope icon"></i> </a>Receiving</li>
            <li><a href="#"> <i class="bi bi-person-check icon"></i> </a>Verification</li>
        </ul>
    </div>
</div>
/*Referencing the class 'icon' with 'li:hover'*/
.wrapper .sidebar ul li:hover, li:hover .icon{
    background-color: #064E88;
    color: white;
}

li:hover .icon as a whole targets the element with the class 'icon' inside the hovered 'li' tag. It is important to separate it with 'li:hover' so that the effect is applied to both the hovered li and <i> tag.

To modify the properties of an element when clicked (or when the tab is selected), simply assign the 'active' class to the selected tab. This class will be removed from the current tab and assigned to another tab when it is clicked.

let tabContainer = document.getElementsByClassName("side")[0]; //This container holds all the tab items.
let tabPane = tabContainer.getElementsByTagName("li"); //Returns a list of all elements with tag name 'li' inside the container, representing all the tab items.

for (let i = 0; i < tabPane.length; i++) {
  //Add an event listener to each tab item to execute a function when clicked.
  //Note: Inside the function, tabsPane[i] refers to the clicked tab item.
  tabPane[i].addEventListener("click", function() {
    tabContainer.getElementsByClassName("active")[0].classList.remove("active"); //Remove the 'active' class from the current selected tab.
    tabPane[i].classList.add("active"); //Assign the 'active' class to the clicked tab item.
  });
};
.wrapper {
  display: flex;
  position: relative;    
}
.wrapper .sidebar{
  position: fixed;
  width: 200px;
  background-color: #00B6E1;
  /* padding: 30px 0; */
  height: 100%;
}
.wrapper .sidebar ul li{
  padding: 15px;
  border-bottom: 1px solid #064E88;
  /* border-top: 1px solid; */
  /* color: white; */
  font-weight: 400;
}
.wrapper .sidebar ul li a {padding: 10px;}

/*Referencing the class 'icon' with 'li:hover'*/
.wrapper .sidebar ul li:hover, li:hover .icon{
  background-color: #064E88;
  color: white;
}
/*Separate properties of the active or selected tab*/
.active {
  background-color: cyan;
}
<div class="wrapper">
    <div class="sidebar">
        <ul class="side">
            <!--Default active tab-->
            <li class="active"><a href="#"> <i class="bi bi-columns-gap fa-lg icon-red icon"></i></a>Dashboard</li> <!-- Additional class 'icon'-->
            <li><a href="#"> <i class="bi bi-graph-up-arrow icon"></i> </a>Reporting</li>
            <li><a href="#"> <i class="bi bi-send-check icon"></i> </a>Sending</li>
            <li><a href="#"> <i class="bi bi-envelope icon"></i> </a>Receiving</li>
            <li><a href="#"> <i class="bi bi-person-check icon"></i> </a>Verification</li>
        </ul>
    </div>
</div>

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

Ways to modify the text color in a CSS animation without using any external libraries?

When using this pure CSS loading animation, the fore color is set to white which may not be visible on a white background. How can the fore-color be changed? .lds-hourglass { display: inline-block; position: relative; width: 120px; height: 120 ...

CSS: Avoiding text overflow in a div container

Utilizing Bootstrap 4, I have a div containing lengthy text that causes overflow within the element. The issue is depicted in this image example. By introducing spaces to shorten words, the text displays correctly without overflowing. However, extended ...

It is not possible to invoke a function within the AJAX success method

When trying to display an error message in my notify (toast) div using jQuery in Ajax success, I am encountering difficulties. Despite decoding the response from the URL properly, only .show() and .hide() functions seem to work. Although I have used conso ...

The BBCode seems to be malfunctioning

I created a custom BBCode for my PHP website to display tabbed content on a page. After testing the BBCode on a separate webpage and confirming there are no errors, I am facing an issue where the tabbed content is not showing up on the actual posting page. ...

How can I update the language of a button text in a React component?

Looking to update the button labels from a different language to English. Here is how it currently appears: https://i.sstatic.net/6JZ6m.png ...

Showing recurring HTML elements with conditional content

I am displaying multiple bootstrap badges in a table column with different colors based on the values. Each badge has a unique class and style name to differentiate the colors. I feel like there is a lot of repetition in my HTML code. Is there a way for me ...

The lifespan of my cookie is limited

I am utilizing the jQuery.min.js from https://github.com/carhartl/jquery-cookie and my cookie code looks like this: $(function() { //hide all divs just for the purpose of this example, //you should have the divs hidden with your css //check ...

Material-UI's JSS does not adhere to the justify content property

Having trouble aligning elements in my React app using material-ui 1.0 and its JSS solutions. The code I've written below should center justify, but it's not working no matter how I configure it. I suspect there might be a naming issue since mat ...

When utilizing PHP Form Validation alongside Javascript, the validation process does not halt even if a

I have been grappling with creating a basic HTML form validation using Javascript After experimenting with various examples, I am still facing the issue of my index page loading upon clicking the button on the form. Despite including "return false" in wha ...

The hover function stops working once the dropdown class has been removed

I am currently experimenting with a bootstrap template. In the navigation bar, there is an option for "Blog" and "Test". For the "Test" button, I decided to remove the li class="dropdown " because I wanted to create a button that changes color on hover si ...

Child element failing to resize along with parent container

Here is the HTML structure I have for a template.php page within my website. <!DOCTYPE html> <html lang="en"> <head> <title></title> ...... </head> <body> <div id="container"> ...

Using jQuery to sift through and sort through content

I have successfully implemented a basic filter using HTML and jQuery. The issue I am facing is that when all filter options are unchecked, it displays no results. My goal is to modify the functionality so that when all checkboxes are deselected, it automa ...

How can you modify a hyperlink URL before it is activated?

I'm facing an issue with a URL structure that needs updating before it is clicked. The current URL looks like this: https://url.com/a b c which is causing redirection problems because the actual link has underscores instead of spaces in its URL. Is ...

Learn how to showcase a text file uploaded to a webpage with NODE js and HTML

<!DOCTYPE html> <html> <body> <form action = "/submit" method = "post"> Select a file: <input type="file" name="file"> <input type="submit"> </form> </bod ...

Trigger a change event for a Material Checkbox by referencing its unique identifier

<div *ngFor="let cus of deselectedList | keyvalue" (click)="clickCheckBox('customer_'+cus.key+'_checkbox')"> {{cus.key}} <mat-checkbox id="customer_{{cus.key}}_checkbox" (change ...

What could be causing this strange striping effect in the radial gradient?

What causes the striped effect in the radial gradient code provided on this website: click here to view body { background: rgba(216,215,221,1); background: -moz-radial-gradient(center, ellipse cover, rgba(enter code here216,215,221,1) 0%, rgba(0,9 ...

Is there a mistake in the way I am creating a horizontal navigation bar?

Currently working on creating a simple navigation bar using HTML/CSS for a Java/Spring Boot project. Admittedly, my HTML/CSS skills are quite limited as you can see below. I'm aware that there might be some mistakes in my approach. Thank you in advanc ...

Issue with CSS Transform Causing Rendering Errors in Both Chrome and Firefox

While a pixel here or there can be overlooked, in this case, precision is essential for the perfect rendering of this simple menu. The spacing between menu items must be impeccably equal to avoid any merging or disjointed appearance. Currently, the sep ...

Field cannot be submitted without the required input after an Ajax request

I want to ensure that all my text/email input forms require a submission before you can "Submit" the email. Unfortunately, when using Ajax to prevent the page from refreshing after pressing the button, the required attribute does not function properly. T ...

"Although disabled, input elements can still be focused on in Firefox browser

Illustrative example let userInput = document.createElement("input"); userInput.id = "user-input"; userInput.type = "number"; userInput.className = "user-number-input"; userInput.disabled = true; document.body.appendChild(userInput); .number-inp ...