The hamburger icon shifts position upon being clicked

My hamburger button is causing me some trouble. Every time I click on it, the position shifts. I need it to stay in place when clicked.

I have shared all my code on jsfiddle.

Click here for code

const nav           = document.querySelector('nav');
const toggleButton  = document.querySelector('.menu-button');
const links         = document.querySelector('.links');

toggleButton.addEventListener('click', () => {
    nav.classList.toggle('nav--active');
    links.classList.toggle('links--active');
});
<nav>
        <a href="#">
            <img src="https://www.designfreelogoonline.com/wp-content/uploads/2017/05/000840-Infinity-logo-maker-Free-infinito-Logo-design-06.png" alt="">
        </a>
        <div class="menu-button">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
        </div>
        <div class="links">
            <ul class="locals">
                <li><a href="#">Home</a></li>
                <li><a href="#">Dining</a></li>
                <li><a href="#">Reservations</a></li>
                <li><a href="#">Menu</a></li>
            </ul>
            <ul class="socials">
                <li>
                    <a href="#">
                        <i class="fab fa-instagram-square"></i>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fab fa-twitter-square"></i>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fab fa-youtube-square"></i>
                    </a>
                </li>
            </ul>
        </div>
    </nav>

Answer №1

The reason behind the upward movement of the button is due to the appearance of the scroll bar when the navbar expands. To resolve this issue, we can make sure the scroll bar always appears even when the navbar isn't expanded. This can be achieved by using the following code:

body 
overflow: scroll

To fix the horizontal movement, we can adjust the top value for the menu button:

top: 1.3rem

For a demonstration of the solution, check out this jsfiddle link: https://jsfiddle.net/6jt4hL9v/

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

Trouble with PHP and HTML Code not functioning properly after loop execution

I created a form with a dropdown menu that pulls options from a MYSQL Database. However, after fetching the options, the button and other components of the code seem to disappear. Everything else works fine when I comment out the PHP Query. <form acti ...

Unique Scrollbar Design for Chrome Extensions

While working on my website, I decided to incorporate a custom scroll bar using a script called fleXcroll. However, I noticed that when clicking inside the scrollable content, a large yellow border appears around the wrapper. This is puzzling because this ...

The website must automatically adjust the size of all elements on the page, including images and fonts

I am currently facing an issue with the resizing of my web page when the user changes the window size or has a different screen resolution. I have created a jQuery function that triggers on resize or page load, which helps in scaling everything proportiona ...

CSS 3 - Apply transition to the 'display' property

I have a question about using the transition effect in conjunction with the display property: Currently, I am testing on Safari. input.input_field { display:none; transition-property: display; transition-duration: 2s; -webkit-transition-p ...

Exploring the contrast in importing CSS between the HTML header, JS file, and Vue Component

Exploring the world of Vue with webpack, I find myself curious about the various ways to import CSS. It appears that there are three methods for importing CSS: Adding style sheets directly to HTML headers: <link type="text/css" rel="stylesheet" ... &g ...

Is there an automatic bottom padding feature?

Currently, I am facing a challenge in fitting the loader into the container without it being overridden by the browser. Using padding-bottom is not an ideal solution as it results in the loader appearing un-resized and unprofessional. Any suggestions or co ...

Animating with linear gradients isn't my strong suit

I attempted to create an animated effect that involves a Tilted & Jump movement across the screen, but unfortunately, the gradient feature is not functioning properly. This is the code I used: <!DOCTYPE html> <html> <head> <sty ...

Set HTML form elements to be shown in 'display only' mode, without any interactivity

In the process of developing a dynamic form builder, I am allowing users to add various blocks of content like buttons, text inputs, paragraphs of text, and images to a page. They can later publish this as a simple HTML form for their use. When it comes t ...

Equal dimensions for all cards in the TailwindCSS gallery display

I am currently working on an image gallery in React with Tailwind CSS. Here's a glimpse of how it looks: https://i.stack.imgur.com/ABdFo.png Each image component is structured like this: <div className="flex items-center"> < ...

CSS - achieving equal height and width for all flex items without specifying fixed values for height and weight

There are 3 flex items displayed here, but the 2nd item has a wider width due to its lengthy description. https://i.sstatic.net/OLy1r.png Is there a way to ensure that all flex items have equal height and width without specifying fixed values, especially ...

Issues arise when attempting to initiate a FastAPI Post Request from an HTML form action located in a separate directory

I'm trying to trigger a FastApi post method from a form submission, but I'm having trouble figuring out how to make it work across different folders. Here is the structure of my project: ├── index.html ├── assets └── backend ...

Implementing a gradient effect on a specific image element within an HTML canvas with the help of jQuery

Currently, I'm working on an HTML canvas project where users can drop images onto the canvas. I am looking to implement a linear gradient effect specifically on the selected portion of the image. My goal is to allow users to use their mouse to select ...

Craft an Interactive Content Carousel

I have been searching for a responsive slide solution for two days with no luck, so I am reaching out for help. I am looking for a simple jQuery code for a slideshow that is lightweight and doesn't require a plugin. Here is the code for the slideshow: ...

Choosing <label> elements, while disregarding those <label> elements that include <input>

I need assistance with CSS rules to target only <label> elements that do not contain an <input> field inside of them. Is there a specific rule I can use for this? <label for="type">Regular Label</label> <label for="type"> ...

Animating the change of background image position in jQuery

Struggling to change the background image position in jQuery animation? Need some assistance to troubleshoot it? Here is the code you are working with: CSS #pnav a{ background:url(http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Ticket ...

Validating button using Java's jsoup library

Looking to determine if a page has a next button for scrolling. The approach is simple: extract the current link, remove the index at the end, replace it with a new index, attempt to connect to the updated link, and if an IOException is thrown, then ther ...

The JSON.parse() method transforms the data within a JSON object

I am currently developing a tool for practicing piano playing in a game. The goal is to help players learn how to play songs by identifying notes that are played within 50ms of each other and displaying them as "you have to press them together." While this ...

What methods can be used to test scss subclasses within an Angular environment?

Exploring different background colors in various environments is a task I want to undertake. The environments include bmw, audi, and vw, with each environment having its own unique background color. Need help writing an Angular test for this? How can I mod ...

ReactJs: difficulty in resetting input field to empty string

I have an application using React v 0.13.1 (Old version). I am struggling to update my input field to "" after retrieving the updated value from the database. Scenario: I am updating the input fields by clicking on the button named "Pull&qu ...

Exporting CSS file from css-loader in Webpack: A step-by-step guide

My application structure is set up like this: /src /app.js /styles.css /images /img.png The content of the app.js file is as follows: const styles = require('./styles.css'); console.log(styles) // => I want a URL to t ...