Bug in Transformation of Menu Icon in CSS

Trying to bring life to a menu icon through CSS transformations on pseudo-elements :before and :after attached to a span.

Everything goes smoothly when the menu icon transitions into its active state, but upon returning to default, the middle bar of the icon reappears abruptly between the other two bars. These extra pixels disrupt the positioning of the bars before the rotation transition, resulting in an awkward animation.

Any ideas on how to solve this issue?

Code snippet:

document.querySelector("#nav-toggle").addEventListener("click", function() {
  this.classList.toggle("active");
});
#nav-toggle {
  display: block;
  width: 80px;
  padding: 0px 15px;
  line-height: 61px;
  color: #fff;
  background-color: #333;
  text-align: right;
}

#nav-toggle span {
  top: 36px;
}

#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
  cursor: pointer;
  border-radius: 1px;
  height: 4px;
  width: 25px;
  background: white;
  position: absolute;
  display: block;
  content: '';
  transition: all 500ms ease-in-out;
}

#nav-toggle span:before {
  top: -8px;
}

#nav-toggle span:after {
  top: 8px;
}

#nav-toggle.active span {
  background-color: transparent;
  top: 36px;
}

#nav-toggle.active span:before,
#nav-toggle.active span:after {
  top: 0px;
}

#nav-toggle.active span:before {
  -webkit-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#nav-toggle.active span:after {
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<body>
  <div>
    <a href="#" id="nav-toggle"><span></span>MENU</a>
  </div>
</body>

Answer №1

Modify the following code:

#nav-toggle span:before {
  top: -8px;
}

#nav-toggle span:after {
  top: 8px;
}

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

Active bootstrap navigation tabs

I am struggling to maintain the active tab in my left navigation tab. The issue arises when I load the page with the first tab active, and then click on another tab - the new tab becomes active, but the first tab remains active as well. I would like only t ...

AngularJS directive that allows for either a click action to be passed or a ui-router state change to

I have a button (an a tag) that is displayed in multiple locations on my website. It is labeled "View Demo", and sometimes it directs to a demo page using an ui-sref: <a class="btn btn-primary" ui-sref="hamburger-push" target="_blank"> View Demo ...

When the script is placed at the end of the file, document.getElementById() is still returning null

I need assistance with running a JavaScript function that is located at the end of my .aspx file. This is the div tag in question: <div id="div_NRContainer" oninit="div_NRContainer_Init" class="panelContainer" runat="server"> Following this, with ...

Is there a way to efficiently navigate a local JSON file using React JS?

What is the best way to extract data from a JSON file and utilize it within my code? I attempted importing the file and logging it in the console, but all I get is Object {}: import jsonData from "./file.json"; console.log(jsonData); This is the content ...

Looping through a Vue Bootstrap modal using a v-for directive

I am working on a Vue Bootstrap modal placed within a v-for loop. I need to replace the '1' in both 'v-b-modal.modal-1' and 'modal-1' with the index value, which I can access as {{ index }} while looping through the items. How ...

Managing a significant volume of form data transmission to PHP backend

I have a moderately large HTML form with Javascript functionality to dynamically create additional 'records' within certain sections of the form. There could potentially be over 50 INPUT elements in total. I am contemplating the most effective a ...

Examining the functionality of my PHP codes and forms

I'm currently working with two HTML forms and two PHP scripts. The first form is designed to save a user's email to a .txt file, while the second form is a Stripe payment form that processes payments using PHP code. However, I've encountere ...

From where does the require.js file originate?

Currently, I am learning Angular 2 from the tutorial available at https://github.com/pkaul/maven-typescript-example. Once I proceed with the 3rd step (mvn jetty:run), a runnable war folder is generated in the example-webapp/target directory. However, I ha ...

Retrieving string array elements within a Vue.js v-for iteration

Trying to extract string values from an array within a function. The component declared in the template looks like this: <video-preview v-for="video in playlist" vid-id="how-to-retrieve-string -value-from-the-current-iteration-in-playlist"></vi ...

Ways to dynamically incorporate media queries into an HTML element

Looking to make some elements on a website more flexible with media rules. I want a toolbar to open when clicking an element, allowing me to change CSS styles for specific media rules. Initially thought about using inline style, but it turns out media rul ...

"An error message stating 'Express: The body is not defined when

I'm encountering an issue while trying to extract data from a post request using express. Despite creating the request in Postman, the req.body appears empty (console.log displays 'req {}'). I have attempted various solutions and consulted s ...

Attempting to send a Promise to another function for it to return, encountering an error of "Unhandled promise rejection"

My goal is to develop a versatile database update function that can be utilized for creating more customized update functions. Within the module database.js, the following code is present: const {Pool,Client}=require('pg'); const pool=new Pool( ...

How can we use Javascript to determine if there are any duplicated IDs within an array containing multiple arrays?

Currently, I'm facing a challenge in identifying duplicated values within an array. Let's consider the scenario where we have an array of arrays: array = [ { id: 123, name: 'Emily', address: 'UK' }, { id: 123, name: ' ...

Error encountered in React.JS App due to MODULE_NOT_FOUND when attempting to run 'npm start'

Upon setting up a new react application, I encountered an error when trying to run the command npm start: PS G:\workspace\WEB\gab-web\client> npm start > [email protected] start G:\workspace\WEB\gab-web\client ...

What is the best way to eliminate the option for editing the profile on the front end of

Is it possible to remove or hide the "edit profile" link on the frontend of Joomla (version 2.5.6) once I am logged in on the site? Thank you ...

I am struggling to create a drop-down menu and need help figuring it out

I've tried looking at other questions but still can't get my code to work. Below is the HTML and CSS I'm working with (feel free to condense or modify as needed). .dropdown-menu li ul { display: none; height: auto; margin: 0; padd ...

Experiencing difficulties accessing Facebook using ngFacebook on angularjs application

I've been working on implementing ngFacebook login into my Angular app, but I'm facing an issue with logging in to Facebook. Even after calling '$facebook.log()', nothing is being displayed in the console. This is a snippet of my Angul ...

What should be placed in the viewRef: MapViewNativeComponentType parameter when utilizing React Native Maps in the current.fitToSuppliedMarkers function?

useEffect(() => { if(!origin || !destination) return; mapRef.current.fitToSuppliedMarkers(["origin", "destination"], { edgePadding: { top: 50, right: 50, bottom: 50, left: 50} }) }, [origin, destination]); I'm currently in t ...

What causes the $router.push method to direct to the /summoner/username/ URL instead of just /summoner/username when using { name: 'summoner', params: { summonerName: summonerName }}?

I am facing an issue while trying to execute a function that redirects the user to another page upon clicking a specific element. The problem arises when using: this.$router.push({ name: 'matchlist', params: { summonerName: summonerName }}) It ...

Having trouble transferring a file using html and php?

Currently, I am attempting to transfer a file from one directory to another using PHP. However, I am encountering an issue where every time I select the file to upload and press submit, it redirects me to a page that saves my .php file instead of the inten ...