Fixed Container housing a Child Div with Scrollbar

I am faced with a challenge involving a lengthy navigation menu (.nav-main) nested within a fixed header (header). When the navigation menu is toggled using jQuery .toggle(), the content extends beyond the window height and does not scroll. I am looking for a solution to add a scrollbar (ideally through CSS) to the child div within the fixed header:

    <header class="clearfix">

                <nav class="nav-main">
                    <button>Open Topics</button>
                    <ul>
                        <li><a href="">Section 1</a></li>
                        <li><a href="">Section 2</a></li>
                        <li><a href="">Section 3</a></li>
                        <li><a href="">Section 4</a></li>
                        <li><a href="">Section 5</a></li>
                        <li><a href="">Section 6</a></li>
                        <li><a href="">Section 7</a></li>
                        <li><a href="">Section 8</a></li>
                        <li><a href="">Section 9</a></li>
                        <li><a href="">Section 10</a></li>
                        <li><a href="">Section 11</a></li>
                        <li><a href="">Section 12</a></li>
                        <li><a href="">Section 13</a></li>
                        <li><a href="">Section 14</a></li>
                        <li><a href="">Section 15</a></li>
                        <li><a href="">Section 16</a></li>
                        <li><a href="">Section 17</a></li>
                        <li><a href="">Section 18</a></li>
                        <li><a href="">Section 19</a></li>
                        <li><a href="">Section 20</a></li>
                    </ul>
                </nav><!--end nav-main-->
</header>

<div id="main">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!--end main-->

The SCSS/CSS:

header{

    background-color: #ccc;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;


    .head-wrap{
        position: relative;
        height: 50px;
    }

    .nav-main{

        position: relative;

        button{
            float: right;
            margin-top: 10px;
            margin-right: 70px;
            background-color: pink;
        }

        ul{
            display: none;
            background-color: #333;
            top: 50px;
            position: absolute;
            width: 100%;
            z-index: 2;
            overflow: auto;

            li{
                background-color: #333;

                a{
                    color: #fff;
                    text-decoration: none;
                    padding:10px 50px;
                    display: block;
                }
            }
        }
    }
}

#main{
    margin-top: 50px;

    p{
        margin-bottom: 20px;
    }
}

And my .toggle() JS:

$(".nav-main button").click(function(event){
        $(".nav-main ul").toggle();
        event.preventDefault();
});

If you have any tips or solutions, please let me know. Thank you!

Answer №1

To ensure proper display, it is recommended to define a height attribute for the .nav-menu ul element. This can be done either statically or through a script that adjusts it according to the window's height.

Check out this JSFiddle for an example

Answer №2

To ensure scrolling, set a specific height for the div element along with overflow:scroll property.

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

Exploring the process of sending props via the render method in Vue.js and observing their behavior

Struggling with passing a prop from a parent component down to a child created using CreateElement/render in vue.js and then watching it. Here is the parent component: Vue.component('my-drawing', MyDrawing) new Vue({ el: '#drawing' ...

Jasmine - verifying variable invocation in test function

As part of my testing process, I am exploring a scenario where a service method is called using a local variable within an angular controller. In this particular case, when the items array is empty, a modal for creating a new item will be triggered via th ...

What is the trick to getting the <label> and <input> elements to show up side by side in an HTML form?

I am designing a registration form for a new website. My goal is to have each label and its corresponding input element displayed on the same line. Here's the CSS code I'm using: #registration-form { background-color: #FFF; height: 600px; ...

Rearrange items in a display: contents wrapper using CSS Grid

I'm having trouble positioning descendants in a root grid while they are enclosed in a container with display: contents. The issue I am facing is that one element is not being correctly placed within the grid: .wrapper { width: 80ch; margin: a ...

Loading AJAX content with jQuery FancyBox after a delay

When a fancybox element is clicked in my app, I use AJAX to load an HTML page. However, on that remote page there is also some JavaScript logic that utilizes AJAX (yes, I know it's strange) to load data upon page load. Currently, I am trying to find a ...

The progress bar in Next JS 13 seems to be malfunctioning and is not displaying any

After transitioning to the new Next JS 13 app directory, I encountered an issue where the progress bar at the top does not function when the page loads. In the previous version, Next JS 12, there was a package named nprogress and nextNprogress that simpli ...

Numerous occurrences of Setinterval

I am currently facing a minor issue with my code. My setup involves an auto-playing rotating fadeIn fadeOut slider, where clicking on a li will navigate to that 'slide' and pause the slider for a specific duration. The problem arises when a use ...

A guide on dynamically checking the checkbox values in each row of a table using JavaScript or jQuery

My table is dynamically populated with values from a database using the following code: var row = table.insertRow(i); i = i+1; // Insert new cells (<td> elements) at the 1st and 2nd position of the new <tr> element: var cell1 = row.insertCell ...

Navigating in a Curved Path using Webkit Transition

Currently, I am working on a simple project to learn and then incorporate it into a larger project. I have a basic box that I want to move from one position to another using CSS webkit animations and the translate function for iOS hardware acceleration. I ...

Exploring the synergies of Next.js and Node.js thread pooling

In my current project, I am utilizing nextJS with NodeJS and looking to implement child processes or workers. The code provided by NextJS itself is functioning perfectly. The relevant code snippets: NextJS with NodeJS Another resource I referred to: Nex ...

What is the process for configuring environmental variables within my client-side code?

Is there a reliable method to set a different key based on whether we are in development or production environments when working with client-side programs that lack an inherent runtime environment? Appreciate any suggestions! ...

Having difficulty utilizing the $.each() function to assign properties to an object

I have an object called habits that contains some values. var habits={ "Drinking":"No", "Smoking":"No" } I want to add the values from this variable into another variable in the following format: var NewHabits = new Object(); Ne ...

Linking two socket.io clients together (Establishing a direct socket-to-socket connection that works across different browsers)

Can a direct P2P connection be established between two browsers using socket.io-client, bypassing the node.js server they are currently connected to? If possible, how? The goal is for clients A and B to communicate directly without going through the node. ...

Enabling Context Isolation in Electron.js and Next.js with Nextron: A Step-by-Step Guide

I've been working on a desktop application using Nextron (Electron.js + Next.js). Attempting to activate context isolation from BrowserWindow parameters, I have utilized the following code: contextIsolation: true However, it seems that this doesn&ap ...

Click on an element that is nested within another element that can also be clicked on

I'm facing a challenge while attempting to create an accordion inside another accordion. The issue arises with the nested elements and their behavior upon clicking. Essentially, I have a parent div with the class .applicant, which expands on click by ...

Tips for confirming date is earlier than current date in Reactjs?

Looking for guidance on how to ensure a date selected by a user is always before the current date when using Material UI in my project. For instance, if it's January 6th, 2021 and the user selects either January 5th or 6th that would be acceptable. Ho ...

Cease the use of jQuery animations

My JavaScript code snippet looks like this: $.get("/<page>.php", "userid='.$userid.'&"+status, function(data){ $("#status").show("fast").html(data).delay(4000).hide("fast"); }); On a page with multiple links triggering thi ...

Display website content with a Bootstrap modal popup

When using the bootstrap modal popup, it's important to ensure that the content displayed does not already exist on the page. The issue of images from the page appearing on the popup is demonstrated in the image linked below. Can someone provide guida ...

The use of jQuery ajax requests is leading to a refresh of the page

I've encountered an issue with a button on my HTML page that is not associated with any form. <input type='button' id='submitter' value='add'/> There is a click handler attached to it: $('#submitter').c ...

I am unable to access the state after setting it in useEffect in React. Why is this happening?

After rendering the component, I utilize socket io to send a socket emit in order to fetch data within the useEffect hook. Subsequently, I also listen for the data that is returned. However, upon receiving the data back from the socket, I update the stat ...