Fixing the position of an HTML element to remain fixed on the screen

Currently, I am working on a responsive website that includes a menu button for mobile devices. Upon clicking this button, users are able to scroll through the menu content. To address the scrolling issue, I have utilized overflow: hidden; and position: fixed;

However, a problem arises as the site unexpectedly shifts to the right and prevents scrolling back. Here is the excerpt of the source code:

    <body>
        <div id="header">
            <div id="logo">
                <a href=""><img src="images/logo.png" alt="logo"/></a>
            </div>
                <div class="menuToggle">
                    <span></span>
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            <div id="nav">
                <div class="aanmelden">
                    <a href="">Aanmelden</a>
                </div>
                    <ul>
                        <li><a href="">Download app</a></li>
                        <li><a href="">Blog</a></li>
                        <li><a href="">Over GYM2Go</a></li>
                    </ul>
                </div>  
            </div>
    </body>

Additionally, here is the JavaScript snippet:

$(window).ready(function() {
    menuToggle();
});

var down = false;

function menuToggle() {
    var i = 0;
    $('.menuToggle').on('click', function(){
        $('.menuToggle').toggleClass('active');
        $('#nav').toggleClass('active');
        $('body').toggleClass('active');

        if (down) {
            $('.lastItem').remove();
            down = false;
        } else {
            $("ul").append('<li class="lastItem"><a href="">Aanmelden</a></li>');
            down = true;
        }
    });

    $('#nav > ul > li').on('click', function(e) {
        $('#nav > ul > li.active').removeClass('active');
        $(this).addClass('active');
        var childUl = $(this).find('ul');
        $('#nav > ul > li > ul').each(function() {
            if (!childUl.is($(this))) {
                $(this).hide();
            }
        });
        if (childUl.length > 0 && childUl.css('display') == 'none') {
            e.preventDefault();
            childUl.toggle();
        }
    });
};

Lastly, the CSS styling used in the project:

body.active {
    overflow: hidden;
    position: fixed;
}

/* Menu */
@media screen and (max-width: 792px) {
    .aanmelden {
        display: none;
    }

    ul {
        display: none;
    }

    .menuToggle {
        display: block;
    }

    #nav {
        margin: 0;
        position: absolute;
        top: 110px;
        background: #e74a2b;
        height: 100%;
        width: 100%;
        max-height: 0;
        overflow: hidden;
        -webkit-transition: width 0.3s; /* Safari */
        transition:max-height 0.3s;
    }

    #nav.active {
        max-height: calc(100% - 110px);
    }

    ul {
    display: block;
    z-index: 5;
    position: relative;
    width: 100%;
    padding: 0;
    margin: 0;
    }

    ul li {
        float: left;
        width: 100%;
        border-bottom: 1px solid rgba(255,255,255,0.4);
    }

    ul li a:hover {
        color: #2c2c2c;
    }

    ul li a {
        float: left;
        width: 100%;    
        color: white;
        padding: 15px 20px;
    }
}

Answer №1

After playing around a bit, I discovered the solution to my issue.

body.active {
    overflow: hidden;
    position: fixed;
    top:0;
    left:0;
}

It seems that the reason for this is because position: fixed; requires a fixed position.

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

How to position a logo and search box in perfect alignment with CSS styling

I'm struggling to align a logo and a search box next to each other. The logo should be on the left and the search box on the right. header { background:#383838; height: 130px; border-top:10px solid #2C2 ...

Flexslider optimized for mobile devices

I am currently using the Sparkling theme on a Wordpress website. This particular theme utilizes Flexslider within a div at the top of the page. Each slide featured in the slider includes an image, as well as a div containing a post title and excerpt. The ...

What is the best way to eliminate the border color on a drop-down menu's bottom border?

I need help removing the border color from a dropdown with the style border-bottom: 1px solid rgba(0, 0, 0, 0.42); After debugging, I discovered that it is originating from the class MuiInput-underline-2593. However, the CSS class MuiInput-underline-2593 ...

Glitch in the animation of the slideshow

I'm encountering a small bug in the animation of my slideshow on the webpage and I can't seem to locate it. I followed a tutorial for the slideshow from this link: https://www.youtube.com/watch?v=TzAshjkhFQw. However, I only want to display 3 sli ...

What is the best method for submitting information using AJAX and jQuery?

Here is the HTML code in question: <script src='https://code.jquery.com/jquery-1.12.4.min.js'></script> <p>1</p> <!-- this content gets loaded with <?php echo $item->id; ?> --> <a id='delBtn1' ...

Shift content from side to side with a click

I've been attempting to shift content left and right using an onclick event and I need it to stop at the margins on the left or right. The list-items are generated dynamically. Here's the code I've tried so far, but it's not quite worki ...

Steps to calculate the total number of rows in a datatable

I have implemented a datatable in my project, and I need to find out the total number of rows in the table when it is loaded. Here's the snippet of code I am currently using: $(document).ready( function() { $('#jobSearchResultTable').data ...

FullCalendar dayClick event fails to trigger any action

I'm having trouble implementing the dayClick function in my fullCalendar. Despite setting up the calendar correctly, nothing happens when I click on a day. Here is the code I am using : var calendar; $(document).ready(function(){ app.init(); ...

What are the reasons behind the occasional failure of vertical centering using height/line-height?

When it comes to CSS single line vertical centering using line-height, I often run into a problem. My usual approach is setting the height and line-height without padding, particularly for buttons: height: 44px; line-height: 44px; However, there are time ...

Unable to properly shut the sliding-over modal

I have implemented a customized version of the codrops slide & push menu (http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/) to create an overlay on my webpage. However, I am facing difficulty in closing it using another link. Any assistance on ...

Display a php page inside a DIV element using jQuery

I'm facing an issue with integrating a PHP page into a div after an Ajax call. My goal is to load page.php into the div with the id of content within the Ajax success function. <div id="content"></div> function load(a, b) { $.ajax ...

Adjusting the dimensions of :before with JavaScript: A Step-by-Step Guide

There's this CSS3 tag named body:before that I'm working with, and I'm looking to adjust the height and width of body:before using JavaScript. Any suggestions on how to achieve this? ...

Responsive Navigation Bar with Bootstrap

Struggling with the Bootstrap 4 navbar issue, specifically trying to close it when the <a> within the <li> tag is clicked. Experimented with data-toggle="collapse" data-target=".navbar-collapse.show" inside the <a> tag and it worked, but ...

How can I ensure that my image remains responsive and aspect ratio-friendly?

Is there a way to accomplish this magical effect? I find myself in a similar situation. I require an image that remains centered while adjusting to fit the screen, all while maintaining its aspect ratio. ...

Presenting field information from a MySQL table by choosing the field name from a dropdown menu

I am trying to populate a drop-down list with field names using the code below. Once a field name is selected, I want to display its contents in a table on the page. The table will have only one column as only one field name can be selected at a time. I ha ...

Reading PHP Variables in Another File Using AJAX, jQuery, or JavaScript

I've been troubleshooting the issue, but it's not working out. I successfully sent a form submission from one.php to two.php using AJAX and everything is functioning properly. Now my goal is to retrieve/read the variable (which was generated in ...

Unable to access HTML elements on an ASP .NET webpage through JavaScript

I've been trying to access the value of a textbox control with the runat="server" attribute, but I'm finding myself stuck searching for a solution. Each time I attempt to retrieve the value, it returns as null. Here is the javascript file: func ...

The collapsed Bootstrap navbar is failing to extend to reveal the menu links

I recently launched a Django website using a Bootstrap template. However, I encountered an issue where the expand menu feature of the navbar does not work properly on mobile devices. Can anyone provide guidance on how to resolve this issue? .nosh_color ...

Using jQuery to duplicate a row and make modifications

I need to duplicate a row, make some modifications to the data, and then add it to the bottom of a table: $(document).ready(function(){ $("#show_new_rows_number").change(function(){ var table_body = $("tbody"); var master_row = table_b ...

Adjusting the selected state of an HTML/CSS checkbox with JavaScript

After downloading a theme with a tailored switch component that replaces the standard checkbox functionality, I noticed that the 'checked' status of the underlying checkbox does not change upon click or touch events. Here is the HTML structure: ...