When utilizing the position: absolute property, scrolling down my content becomes challenging

I have encountered a challenging issue that I am struggling to resolve. Currently, I am working on developing a left side menu using jQuery and I have managed to get it up and running.

However, the problem arises when I set my div #content with only position:fixed. While this keeps the menu working properly, it prevents me from scrolling through the rest of the content on the page.

#content {position:absolute; position:fixed;}

If I add both position:fixed and position:absolute to my content, I can then scroll down the content as desired. However, this setup causes confusion because it allows access to both the menu items and the content simultaneously.

My goal is to have the content fixed only when the side left menu is open and allow scrolling otherwise. Does anyone know how I can achieve this?

You can view my code on JSFiddle: http://jsfiddle.net/3Gezv/9/

Here's a snippet of my HTML:

<div id="menu">
        <ul>
            <li><a href="#">Menu Item 1</a></li>
            <li><a href="#">Menu Item 2</a></li>
            <li><a href="#">Menu Item 3</a></li>
        </ul> 
</div>
<div id="content">
    <div id="menubar">
        <div id="open_menu">Menu</div>
    </div>
</div>

And here's a snippet of my CSS:

* {
    padding: 0px;
    margin: 0px;
}

#menubar {
    width:100%;
    background-color:gray;
    color: #fff;
    padding: 10px;
}

#open_menu {
    cursor:pointer;
}

#menu, #content {
    display:inline;
}

#menu li a {
    padding: 10px;
    display: block;
}

#content {
    width:100%;
    background-color: #fff;
    z-index: 5;
    position: fixed;
    left: 0px;
    height: 100%;
    -webkit-box-shadow:  -5px 0px 4px 0px rgba(0, 0, 0, 0.2);
    moz-box-shadow:  -5px 0px 4px 0px rgba(0, 0, 0, 0.2);
    o-box-shadow:  -5px 0px 4px 0px rgba(0, 0, 0, 0.2);
    box-shadow:  -5px 0px 4px 0px rgba(0, 0, 0, 0.2);
}

#menu {
    float:left;
    width: 350px;
    height: 100%;
}

#menu li {
    background-color:gray;
    border-bottom: 1px solid #888;
}

Answer №1

Avoid positioning the main content of your page absolutely.

Here's a method to achieve your desired outcome:

Make adjustments to the CSS in this fiddle: http://jsfiddle.net/Varinder/9mw8r/1/

Relevant CSS:

#menubar {
    /*width:100%;*/
    ... some styles ...
    position:fixed;
}


/*
#menu, #content {
    display:inline;
}
*/


#content {
    /*width:100%; block elements will be full width by default*/
    z-index: 5;
    /*position: fixed;*/
    position:relative; /*so content will go above menu*/
    ...
}

#menu {
    /*float:left;*/
    width: 350px;
    height: 100%;
    position:fixed; /* menu will stick to sidebar regardless of scroll */
    top:30px; /*so content will not go behind menubar*/
}


.news {
    padding-top:50px; /*so content will not go behind menubar*/
    min-height:900px; /*to create a fake long page*/
}

body {
    overflow-x:hidden; /*will avoid horizontal scrollbar when menu is opened, can be critical. Best not to use it as it may cause issues with webkit-overflow touch property (http://css-tricks.com/snippets/css/momentum-scrolling-on-ios-overflow-elements/)*/
}

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

React issue: Unable to locate jquery module

I recently installed jQuery using npm, and now I am attempting to import it in my React app's App.js file like this: import $ from 'jquery'; However, I keep getting the error message: Module not found: Can't resolve 'jquery&apos ...

What is the most effective approach for managing ResponseEntity on the user interface?

I need to display all posts written by a user and their friends on the user's page after it loads. What is the most straightforward way to achieve this on the UI side? I am using Thymeleaf with Spring. @RequestMapping(method = RequestMethod.GET, pat ...

There is a delay in updating ng-if/ng-hide in real time on the HTML page

Assistance needed for implementing a slight adjustment in AngularJS with TypeScript. The requirement is to change the text of a button for 3 seconds upon clicking, then revert back to its original text. Two HTML elements are created for this purpose, each ...

Removing multiple rows from a local JSON string within JQGrid

In my Jqgrid application, I am looking for a way to efficiently delete multiple rows using row IDs stored in local JSON data. For example: Let's say I have the following row IDs stored in an array: rowids="1,2"; I need to remove these two rows (wi ...

Exploring the Depths of Nested Arrays: A Guide to Efficiently

My array has a nested structure like this: "meetingTime":[ [ { "date":"2021-05-30", "startTime":"15:30", "endTime":"11:11&quo ...

The moving background is not remaining still

Hi everyone, I'm currently in the process of creating a new background design for my website and I want to add some animation to it. You can view my progress here: One issue I am facing is that the "background" div does not remain fixed when scrollin ...

Require assistance in accurately assigning a date to a Date[] in Typescript Array without altering current elements

In my current code, I have a loop that verifies if a date is a holiday and then adds it to an array. The issue I'm facing is that whenever I assign a new element to the array, all previous data in the list gets updated to the latest element. Here&apos ...

Animate the smooth transition of a CSS element when it is displayed as a block using JQuery for a

I have implemented the collapsible sections code from W3 School successfully on my website. Now, I am trying to achieve a specific functionality where the "Open Section 1 Button" should slide down with a margin-top of 10px only if the first section "Open S ...

WordPress Ignoring PHP Generated Elements When Processing Divs

Whenever I attempt to utilize the get_date function, it seems to disregard all div elements and instead places itself right after the header, at the beginning of the <div class="entry-content">. <div class="entry-content"> May 16, ...

Steps to ensure a variable remains constant across a controller, JS file, and a rendered partial

I am working on rendering a partial viewer using ajax and jQuery. My goal is to assign a variable within the partial based on the link that is clicked. Here is the ERB code: <%=link_to "link1", viewer_path(id: Painting.first), remote: true%> <%= ...

jQuery Swiper slider

I am currently working with the Swiper jQuery slider for my project. As a beginner in programming (js / jquery), I am looking to run a specific function, involving some jquery code, whenever the first slide of the slider is active. It seems that this can ...

What is the best way to apply a JQuery UI Tooltip to a newly added element?

In my HTML file, I have included all the necessary JS and CSS files. Then I load a content page within it. Some of these content pages contain Usernames where I want to implement a tooltip feature. Here is an example of how they are structured: <user ...

Using jQuery to extract contact information from Google: A step-by-step guide

I'm currently using Google's API to access my contact list information and after logging the output in the console function fetch(token) { $.ajax({ url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token. ...

Manually reloading the page causes issues with AngularJS routing functionality

I've been attempting to manually reload the page from my browser, but unfortunately it's not working and is displaying an error message: Cannot GET /rate/4 This is what my route setup looks like in Angular: angular.module('routing&ap ...

I am attempting to format an HTML form with proper indentation, but it seems to be eluding me

Struggling to design a custom registration form template, I'm facing an issue where paragraph spaces or indents are not being recognized by the program, even in the preview pane. Seeking assistance with this problem. I am attempting to position this ...

When combining AJAX with PHP and HTML, one limitation to note is that PHP alone cannot generate or create the file

I am facing a particular problem here. Currently, I am using HTML with AJAX: <html> <head> <script> function ajax_post(){ // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Create some variables we need to ...

The absence of the Three.js file in my HTML file is noticeable

Currently, I am diving into HTML5 and experimenting with the Three.js 3D engine. While following a tutorial from this source, it was recommended that I include the three.js file in my HTML document. However, I encountered two files with the same name and d ...

The sluggish rendering speed of AngularJS is causing a delay in displaying the loader

Within my application, I have tabs that load a form in each tab with varying numbers of controls. Some tabs may contain a large number of controls while others may have fewer. My goal is to display a loader while the contents are being rendered into the HT ...

Excessive gap beneath the footer in Wordpress

I have come across many solutions for this issue, but none of them seem to work for me. On my main page, when I scroll to the bottom, the footer appears to have extra space beneath it, which actually seems to be the background of the page. This problem onl ...

Methods for obtaining a directive's scope value and utilizing it in another directive within angularjs?

I have a specific requirement to transfer the scope of one directive to another directive and enable two-way binding. The goal is to update JSON values whenever the ng-model changes. In the example below, the JSON contains rowsets with attributes that nee ...