What are the steps to customize the color of an icon and text when clicked or when hovering over them

I just entered the world of coding and am currently immersed in a project. Right now, I have a side navbar and I'm attempting to change the color of the icon and text when I hover over it or click on a route. For instance, when I click on the Home link, I want it to stay blue until I click on another link, while also keeping it blue when hovering over other links.

Below is the code for my side navbar:




const Navbar = ({ handleClick, isLoggedIn, email }) => (
  <div className="wrapper">
    <nav
      className="navbar navbar-expand d-flex flex-column align-item-center-start"
      id="sidebar"
    >
      <a href="/" className="navbar-brand text-light mt-2">
        <div className="display-6 font-weight-bold">
          <span>SPODify +</span>
        </div>
      </a>
      <ul className="navbar-nav d-flex flex-column w-100 mt-4">
        <li className=" h-25 nav-item border-bottom">
          <a href="/" className="nav-link text-light pl-4">
            <i className="bi bi-house-door "></i>
           HOME
          </a>
        </li>

        <li className="h-25  nav-item border-bottom">
          <a href="#" className="nav-link text-light ">
            <i className="bi bi-search"></i>
            SEARCH
          </a>
        </li>

        <li className="nav-item h-10 border-bottom">
          <a href="/show" className="nav-link text-light ">
            <i className="bi bi-rainbow"></i>
            PODCASTS
          </a>
        </li>

        <li className="nav-item h-25 border-bottom">
          <a href="#" className="nav-link text-light pl-4">
            <i className="bi bi-collection"></i>
            YOUR LIBRARY
          </a>
        </li>

        {isLoggedIn ? (
          <>
            <li className="nav-item h-25 border-bottom">
              <a href="/login" className="nav-link text-light pl-4">
                <i className="bi bi-person-circle"></i>
                {email}
              </a>
            </li>
            <li className="nav-item h-25 border-bottom">
              <a
                href="#"
                onClick={handleClick}
                className="nav-link text-light pl-4"
              >
                LOGOUT
              </a>
            </li>
          </>
        ) : (
          <li className="nav-item h-25 border-bottom">
            <a href="/login" className="nav-link text-light pl-4">
              LOGIN
            </a>
          </li>
        )}
      </ul>
    </nav>
    <div>
      {isLoggedIn ? (
        <div>
          <Home email={email} />{" "}
        </div>
      ) : (
        <div>
          <h1>WELCOME, SIGN UP</h1>
          <Signup />
        </div>
      )}
    </div>
  </div>
);


I tried to achieve this by adding this to my CSS file:



nav a.nav-link:hover {
  background-color: blue;
}

But this just makes the entire section blue :

https://i.sstatic.net/SXsAE.png

Any tips on how I can change ONLY the icon and text when I hover over it and once I click on the link? Thanks in advance!

For reference, this is what I'm trying to achieve:

Currently clicked on Home link and hovering over Guests that's why it's both a different color

https://i.sstatic.net/0Ypzx.png

Answer №1

You're headed in the right direction. To change the color of the text and icon, you'll need to adjust the background-color to color in this CSS code snippet:

nav a.nav-link:hover {
  color: blue; 
}

Example:

a:hover{
  color: blue;
}
<ul>
  <li>
    <a>My Test</a>
  </li>
</ul>

For the active state after clicking, you will need to add an additional class (e.g., .active). You can then style it the same way as when hovering:

a:hover,
a.active{
  color: blue;
}

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

Tips for automatically displaying user text based on its natural direction

Looking for a way to properly display user inputted text in Unicode based on its general direction, rather than defaulting to left-to-right. An issue arises when dealing with Arabic text as shown below, where the English word (4th) is split apart: اُر ...

How to troubleshoot Javascript code that fails to identify if a color is white

What could be causing my code to malfunction? I am attempting to determine if the paragraph text color is white (as determined by CSS settings). If it is indeed white, I want to change it to black for better visibility. function adjustColor() { if (d ...

Free up MySQL connections within a Promise.All implementation

At the moment, I am facing issues with releasing MySQL connections from a connection pool. Interestingly, when I release connections in a synchronous "for" loop, everything works fine. However, when I attempt to release them asynchronously using Promise.Al ...

Having difficulty getting a basic code to work: ajax method ($.post) with MySQL is not

I am facing an issue with this code not functioning properly. I want to display the result of a MySQL query without sending any data using my simple Ajax code. $('.myClass').on('click',function(){ $.post('resultAll.php'); ...

Tips for resolving the error message "CORS policy blocking XMLHttpRequest access while trying to retrieve data from an API"

I've scoured through numerous similar questions without finding a solution that works for me. In the process of creating a React application, I'm attempting to retrieve data from my university's library API, which is in XML format. To conve ...

How can I ensure that my navbar-right remains fixed to the right side of the screen and is always visible at the top of the page as I

When the page first loads: After scrolling down: I am attempting to create a fixed navigation bar at the top of the screen, so that it remains visible as the user scrolls down the page. However, I am facing an issue where my navbar brand is positioned to ...

Error: Attempting to access property 'baseHref' of an undefined value is not possible

Creating a webpack configuration for Angular 7 has been my recent project. I found a helpful tutorial on how to customize build configuration at this link. I decided to use the @angular-builders package for this task. Upon checking my index.html file, I m ...

How to center align text in the media-body using Bootstrap 4 Beta

Struggling to vertically align the text in media-body to the middle when using the new Bootstrap 4 beta version. Tried an approach mentioned in this solution, but it didn't work with the latest version of Bootstrap. Attempted using text-center and a ...

Is it necessary for me to use bindActionCreators?

While going through a post, I couldn't help but notice that the bindActionCreators method from redux wasn't being utilized. Is there a specific reason for this? Could it be that the method is not necessary in this context? The post in question ...

Converting a JavaScript function to TypeScript with class-like variables inside: a step-by-step guide

During the process of converting a codebase to TypeScript, I encountered something unfamiliar. In particular, there are two functions with what appear to be class-like variables within them. The following function is one that caught my attention: const wai ...

Tips for accurately aligning a relative p tag within a td

Description: I am trying to prevent text overflow in a table cell and position the text underneath a header without wrapping it under an image. I have tried setting a static width for the image and adjusting the left property, but it is not working as expe ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...

The switch case functionality refuses to change when I interact with the user interface

Can someone please help me troubleshoot? I'm not receiving any errors in the Chrome console. HTML <div class="wrapper"> <i id="repeat" class="fas fa-stop-circle"></i> </div> Javascript const wrap ...

Strategies for Handling Various Versions of npm Modules within a Project when Multiple Packages Depend on Specific Versions Internally

I find myself in a predicament with my main React project using version "1.5.1" of "@material-ui/core", while attempting to build a new component that requires version "3.2.1" of "#@rjsf/material-ui" which internally relies on the latest version of "@mater ...

When I try to execute a mutation with Apollo and React, I encounter a 400 error. It could be due to a

Every time I try to perform a mutation, I keep getting a 400 error code for some strange reason. Here is my httpLink code snippet: // ApolloProvider.js const httpLink = createHttpLink({ uri: 'http://localhost:3000/graphql', }); const client ...

The Angular 4 application encountered a TypeError due to being unable to access the 'setupScene' property of an undefined object

How can I execute an Angular class function within the Load function in TypeScript? I am encountering an error when trying to call a method within the load method in Angular CLI, resulting in an undefined function error. import { Component, OnInit ,Elemen ...

How to troubleshoot when Reactjs doesn't render anything after successfully posting a new note using axios to the server?

I am currently working on updating data in the backend using a json server. I've noticed that when I refresh the server, the new data gets posted successfully; however, the render of the updated list disappears from the screen after submitting. Can an ...

Customize CSS styling

When it comes to the styling of my first line, I have a specific style in mind: * { text-align:left; } This style works well across most parts of the site as they are predominantly left aligned. However, there are few areas where I need the text alig ...

How can I trigger a re-render following the execution of renderEditCell in the MUI Data Grid?

I'm currently facing an issue where editing a cell works fine, but I have to reload the page to see the new value. Is there a way to re-render the cell without refreshing the entire page? I've already attempted to update the rows using apiRef.cu ...

Create a line break in the React Mui DataGrid to ensure that when the text inside a row reaches its maximum

I'm facing an issue with a table created using MUI DataGrid. When user input is too long, the text gets truncated with "..." at the end. My goal is to have the text break into multiple lines within the column, similar to this example: https://i.sstati ...