Is there a way to make all the burger bars change color when hovered over in my ReactJS/NextJS application?

Recently, I encountered an issue with my hamburger menu. When I hover over one of the bars, only that specific one changes color and not all of them. I am struggling to figure out how to make all the bars change color when any one of them is hovered over.

How can I make all the hamburger bars change color when any one of them is hovered over?

This is the HTML code for the hamburger menu:

<div className="burger-menu" onClick={updateMenu}>
    <div className={burgerClass} ></div>
    <div className={burgerClass} ></div>
    <div className={burgerClass} ></div>
  </div>

Here are the CSS styles for the hamburger menu in Bootstrap 5:

.burger-bar:hover {
  background-color: #ff0000;
}

@media (max-width: 550px) {
  .burger-menu {
    display: inline;
    height: 100%;
    width: 3em;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: space-between;
    cursor: pointer;
  }
  
  .burger-bar {
    display: inline;
    width: 3em;
    height: 0.5em;
    background-color: #292f36;
    border-radius: 0.5rem;
    margin: 2px;
  }

  .nav-item {
    display: none;
  }
}

@media (max-width >= 550px) {
  .burger-menu {
    display: none;
  }
  
  .burger-bar {
    display: none;
  }
}

Answer №1

After some experimentation, I successfully customized the appearance of the hamburger icons by assigning each a unique identifier and adjusting their colors using onMouseOver and onMouseLeave events.

const changeBackgroundRed = () => {
    document.getElementById('barOne').style.backgroundColor = '#ff0000';
    document.getElementById('barTwo').style.backgroundColor = '#ff0000';
    document.getElementById('barThree').style.backgroundColor = '#ff0000';
};

const changeBackgroundGrey = () => {
    document.getElementById('barOne').style.backgroundColor = '#292f36';
    document.getElementById('barTwo').style.backgroundColor = '#292f36';
    document.getElementById('barThree').style.backgroundColor = '#292f36';
};


<div className="burger-menu" onClick={updateMenu}>
    <div id='barOne' className={burgerClass} onMouseOver={changeBackgroundRed} onMouseLeave={changeBackgroundGrey}></div>
    <div id='barTwo' className={burgerClass} onMouseOver={changeBackgroundRed} onMouseLeave={changeBackgroundGrey></div>
    <div id='barThree' className={burgerClass} onMouseOver={changeBackgroundRed} onMouseLeave={changeBackgroundGrey></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

I'm wondering, on a storybook, where is the best place to set my MUI X license key

Can you help with where to specify my license key in Storybook? I need guidance on where to set the license key in Storybook. According to MUI Docs, it should be done before React renders the first component and only once in the application. However, I ...

Creating a Robust Next.js Build with Tailor-Made Server (Nest.js)

I'm in need of assistance with compiling/building my project using Next.js while utilizing a custom server. Currently, I have integrated Nest.js (a TypeScript Node.js Framework) as the backend and nested Next.js within it. The integration seems to be ...

Warning in Next.js: When utilizing conditional rendering, the server HTML is expected to have a corresponding <div> inside another <div>

Although similar questions have been asked on various platforms like Google, none seem to provide answers that align with my specific situation. Essentially, my goal is to have a different search bar displayed in the header based on the page I am currentl ...

The getServerSession() method in NextAuth fails to retrieve all of the user fields when called in an API route

I am currently working on implementing an API route where I need to verify the user's authentication status and check if they are an admin. As part of this process, I attempted to utilize the getServerSession method, however, it returned a session wit ...

Unable to locate the root element for mounting the component in Cypress with React

I am currently testing my react app, which was created using create-react-app, with the help of cypress. Unfortunately, I encountered an error that looks like this: https://i.stack.imgur.com/xlwbo.png The error seems to be related to trying to fetch an ...

Connect Datadog to NextJs using the app router

I am facing a challenge with integrating Datadog into my NextJs 14 app that uses the app router. In previous versions, I successfully integrated Datadog into React and NextJs apps without the app router by placing initialization code in the top level compo ...

Encountering mysterious state objects when using React with Redux

When working with reducers, I encountered an issue where the state was being altered unexpectedly upon mapping it to props, resulting in additional unknown objects! Snippet of my code Reducer & store: const reducer = async (state = { dataList: [] } ...

What could be causing my page to suddenly disappear?

After saving my changes in the IDE and refreshing the browser to test the prompt() function in my index.js file, the entire page goes blank, showing only a white screen. I double-checked the syntax of my function and it seems correct. Everything else on th ...

Redux (RangeError): The call stack has exceeded its maximum size limit

After successfully adding Redux for state management in my project, everything was working fine until I introduced reducers and middleware to fetch data. The problem arose when I started running the app and using dispatch (not necessarily on new actions), ...

Is there a way to execute a function once the component has finished

Is there a way to ensure a vanillaJS function is called after the component has fully rendered in React? I am encountering errors because it runs before the component loads and cannot find specific documents. How can I execute this code only after the com ...

Why is the useHistory hook in React failing to function properly?

When I try to launch my web application using npm start, an error occurs stating that the useHistory hook has not been downloaded, despite having installed the latest version of react-router-dom. Can someone explain why this is happening? Here is a screens ...

What methods can I use to minimize the duration of invoking the location.reload() function?

When I'm using window.location.reload() in my onClick() function, it's taking too long to reload. I tried modifying the reload call to window.location.reload(true) to prevent caching, but it's still slow. The issue seems to be with location. ...

The toggle-input component I implemented in React is not providing the desired level of accessibility

Having an accessibility issue with a toggle input while using VoiceOver on a Mac. The problem is that when I turn the toggle off, VoiceOver says it's on, and vice versa. How can I fix this so that VoiceOver accurately states whether the toggle is on o ...

Upon modifying the selection, I am notified to utilize the 'defaultValue' or 'value' props on the <select> tag, rather than applying 'selected' on the <option> tag

I am currently working with a material select component: <FormControl fullWidth variant="outlined" className={classes.formControl}> <InputLabel ref={inputLabel} htmlFor="outlined-age-native-simple"> Filial </InputLabel> <Sel ...

Every time I attempt to set up Material-UI, a barrage of errors is thrown my way, making the installation process frustrating

After successfully installing React using npx, I encountered issues when trying to install Material-UI. Below is the code snippet that shows the errors: npm install @material-ui/core npm WARN saveError ENOENT: no such file or directory, open '/Users ...

Is it possible to create a development build using Npm with React and Typescript?

I have successfully set up a TypeScript React app by using the command below: npx create-react-app my-app --template typescript However, running "npm start" generates development javascript files and launches a development server which is not id ...

Launching the date/time selection tool through code in the Material UI framework

I'm currently working on a project that involves React and Material Design. I decided to utilize Material UI for its sleek Dat- and Time Pickers components. In my application, I have to set Start and End Times and Dates in a specific order. To stream ...

Tips for streamlining responses for the latest create-next-app version

After running npx create-next-app@latest, a series of questions are prompted that disrupt our docker pipeline automation. Is there a way to automate the response to these questions? Currently, we have this command in our Dockerfile: RUN npx --yes create- ...

What is the method to initialize a Stripe promise without using a React component?

I have encountered an issue while implementing a Stripe promise in my React app. The documentation suggests loading the promise outside of the component to prevent unnecessary recreations of the `Stripe` object: import {Elements} from '@stripe/react-s ...

Toggling between ascending and descending order on table header click in React

It seems like a simple task, but I am struggling to grasp the solutions I have come across. I currently have a function set up that changes the column order when I click on the header. However, I would like the second click to reverse the order. This is t ...