Instructions on how to activate scrolling for a div element that becomes visible upon clicking a button

When a button in the navbar is clicked, a div appears with a height that exceeds the viewport/page size. How can I enable scrolling for the entire page when this happens?

I attempted to use overflow: scroll, but it only applied scrolling to the internal div displayed upon button click. Is there a way to make the entire page scroll instead? Any suggestions on how to achieve this?

You can view an example on Plunker here: https://plnkr.co/edit/JzGCix39dSd1q7KOiNTc?p=preview

Here is the code snippet:

<body>
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li>
              <a href="#" id="showKoo" onclick="showKoo()">Tool bar</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <div class="mainBorder topcorner complete_width complete_height" id="box" style="display: block; background-color:red">
      <h4> SOME CONTENT</h4>
    </div>
  </body>

CSS:

body{
  overflow: scroll;
}

.centered_div{
    margin: 0 auto;
}

.mainBorder{
    border: 1px #B2B9BA solid;
    overflow:scroll;
}

.topcorner{
    position:fixed;
    top:41px;
    right:30px;
}

.complete_width {
    min-width: 360px;
    max-width: 360px;
}

.complete_height {
    min-height:640px;
    max-height:640px;
}

JavaScript:

function showKoo() {
    $('#box').toggle();
}

Answer №1

There is no need to specify overflow for the body tag, as it will automatically add scroll functionality based on the content size.

Consider using position:absolute instead of fixed positioning.

// JavaScript code snippet

function showKoo() {
    $('#box').toggle();
}
/* CSS styles */

body{
  position:relative;
}

.centered_div{
    margin: 0 auto;
}

.mainBorder{
    border: 1px #B2B9BA solid;
    
}

.topcorner{
    position:absolute;
    top:41px;
    right:30px;
}

.complete_width {
    min-width: 360px;
    max-width: 360px;
}

.complete_height {
    min-height:640px;
    max-height:640px;
}
<!DOCTYPE html>
<html>

  <head>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d9c2c6d6c1caf3809d839d839d">[email protected]</a>" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
    <link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3934342f282f293a2b763828281b687568756a">[email protected]</a>" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li>
              <a href="#" id="showKoo" onclick="showKoo()">Tool bar</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <div class="mainBorder topcorner complete_width complete_height" id="box" style="display: block; background-color:red">
      <h4> SOME CONTENT</h4>
    </div>
  </body>

</html>

Answer №2

The body usually adjusts its height based on the content it contains, unless you start manipulating the position of certain child elements.

Here, your container has been given a "position: fixed" property under ".topcorner".

As a result, this element is taken out of the normal document flow, causing the body to ignore its height.

Is it absolutely necessary for that element to have 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

Issues with babel plugin-proposal-decorators failing to meet expectations

I recently included these two devDependencies in my package.json: "@babel/plugin-proposal-class-properties": "^7.1.0", "@babel/plugin-proposal-decorators": "^7.1.6", In the configuration file .babelrc, I have listed them as plugins: { "presets": ["m ...

Encountering a hydration issue in Next.js when attempting to refresh the page after switching languages (excluding English) with next-translate/useTranslation

I've encountered an issue with the useTranslation hook from the next-translate package in my Next.js project. Although all languages are being recognized, I'm facing a hydration error whenever I change the language and refresh the page. Below is ...

Navigating between pages using React and Material UI

My concept involves a <LandingPage> with a <Top> element that can be modified by user interaction. The <Top> component contains a pageState, which can be changed by clicking buttons on the First and Second pages. Is this considered good ...

Changing Settings on the Fly with JQuery

I am facing an issue with a table that has values generated dynamically using PHP, such as id and name attributes (e.g. id="question_". My challenge is how to set an element attribute considering this dynamic nature. For instance, I need to update the tex ...

Anti-virus programs are preventing long-standing AJAX connections

Hey there, I have come across something quite strange while developing a web application that relies on long-held HTTP connections using COMET to stream data between the server and the application. The issue I've encountered is that some anti-virus p ...

Choose the list item by identifying the corresponding unordered list

Is there a way to target the second li element within an ul list like this: HTML <ul> <li></li> <ul> <li>This one is what I need to select</li> </ul> </ul> ...

Is it feasible to automate the cropping process with selenium?

Exploring a new challenge: simulating a cropping feature. I understand that replicating human cropping is impossible, but the image I intend to crop remains a fixed size each time I run the test. My goal is to establish the cropping WebElement at a constan ...

Guide to custom sorting and sub-sorting in AngularJS

If I have an array of objects like this: [ { name: 'test1', status: 'pending', date: 'Jan 17 2017 21:00:23' }, { name: 'test2', sta ...

"Enhancing Functionality: A Detailed Guide on Callback Functions in Express

Currently, I am expanding my coding skills by learning express/Node, transitioning from C and PHP/MySQL. The MDN tutorial on express has been incredibly helpful in my learning journey, making everything straightforward. Thanks to the Mozilla teaching team, ...

Attempting to show the name in AngularJS

I've been working on mastering Angular JS, but I'm facing a challenge with displaying the user I just added to the database on the next page. While I can display other users, the newly added user's data just won't show up! I've tri ...

Discovering the quantity of identical elements within a JavaScript array

Looking for some assistance in solving a JavaScript problem as I am relatively new to the language: I have an array and I need help in determining the count of identical values. Below is my array: var arr = ["red", "blue", "green", "red", "red", "gray"] ...

When using props.onChange(e.target.value) in a textField component in Material UI, it unexpectedly returns an object instead of a value

function FormInput(props) { const classes = formInputStyles(); return ( <div> <TextField onChange={(e) => props.onChange(e.target.value)} InputProps={{ classes, disableUnderline: true }} {...pro ...

What is the best way to handle an AJAX POST request and a resource controller in Laravel version 5.6?

As a newcomer to web development, I am currently learning about JavaScript and AJAX. Through various iterations of the AJAX call, I have managed to grasp the basics of how it works. My current goal is to send the content of my form to the database. Here ...

Tips for achieving horizontal alignment of headers

My webpage has two headers positioned as 'CompanyName' on the top left and 'Date' at the top middle but I am struggling to align them horizontally. I attempted to enclose them inside a div element, however, this did not work. Can someon ...

Combining JSON fields and searching based on the combined value within MarkLogic

In the JSON format provided below, I have stored a person's first name and last name along with their nickname. { "Person": { "Records": [ { "nameType": "Primary", "firstName": "Sagar", "lastName" ...

Learn how to retrieve values from a .json file in real-time and then perform comparisons with user input using Python

I have a JSON file structured like this: [ { "name": { "common": "Aruba", "official": "Aruba", "native": { "nld": { "official ...

Having trouble importing from the public folder in CSS with Create React App?

While working on a project initialized with Create React App, in the public folder there is an assets directory containing a file named logo512.jpg. When I use this file in a component like so: <div> <img src='/assets/logo512.jpg'/& ...

What is the difference between a CSS background image and a default background?

I am faced with a challenge concerning a div element that has both a background color and an image with a transparent background. My objective is to have the default background color of the div be white, while having a different color as the background fo ...

reducing the dimensions of the expanding panel in Material UI

I am facing a challenge which requires me to reduce the size of the expansion panel when it is open or expanded. I checked the elements and styles tab, but it seems that we need to override the styles. Has anyone dealt with this scenario before? Here is a ...

challenges with Next.js dynamic routing when dealing with spaces

I am facing an issue with accessing dynamic routes using a product name from my API when the name contains spaces. However, I can access the routes without any problem if the product name is just a single word. utils/api.js export async function getProduc ...