Creating a scrollable div within a Bootstrap grid without affecting the scrollability of the overall page can be achieved by

I am currently in the process of developing a web application using Bootstrap 4. The main page should remain static without any scrolling, while the child panels should be able to scroll if their content exceeds the available space.

My goal is to achieve this effect similar to the image below, without relying on jQuery as shown in the demo: https://i.sstatic.net/DWJVi.png I am looking for a CSS solution, possibly using flexbox

If I remove the jQuery code, the div extends beyond the app area, causing unwanted scrolling as depicted in the following image: https://i.sstatic.net/iS6pd.png

I attempted to add the following styles to the red div:

max-height: calc(100% - 20px);
overflow-y: auto;

However, the percentage calculation did not work as expected. Using a fixed value like max-height: calc(210px - 20px); does work but breaks when resizing the window.

Is there a straightforward (or complicated if necessary) way to achieve this layout purely with CSS?

Check out my CodePen here

Answer №1

To address your issue, you may need to apply the CSS property overflow:scroll and ensure that one of the ancestor elements has a defined height for max-height to function properly. Here is an example with the necessary CSS code:

.main_container {
  width: 100%;
  /* Set the height to 100% of the viewport */
  height: 100vh;
  /* Hide overflow to disable scrolling by default */
  overflow: hidden;
  display: flex;
  background:#efefef;
}
/* Apply scroll to designated container */
.notes_scroll {
  height: 100%;
  width: 100%;
  padding: 25px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
}
/* Hide scrollbar */
.notes_scroll::-webkit-scrollbar {
  display: none;
}
.sec {
  flex-basis: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.left {
  max-width: 30%;
  background: #dadada;
}
.right {
  flex-wrap: wrap;
}
.top {
  height: 50%;
  width: 100%;
}
.notes {
  max-width: 50%;
  height: 50%;
  background: #fff;
}
.bottom_right {
  max-width: 50%;
  height: 50%;
  background: #ccc;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="main_container">
  <div class="sec left">Left</div>
  <div class="sec right">
    <div class="sec top">Top</div>
    <div class="sec notes">
      <div class="notes_scroll">
        <h2>Notes</h2>
        <div class="notes_content">
          Many notes...<br/> Many notes... (repeated for visibility)
        </div>
      </div>
    </div>
    <div class="sec bottom_right">Bottom Right</div>
  </div>
</div>

Answer №2

Big thanks to @return_false for pointing me in the right direction. I noticed that their answer didn't include Bootstrap, which was a crucial element mentioned in the post title and first sentence. I prefer answers with minimal CSS, utilizing Bootstrap classes where possible. Additionally, I wanted the notes div to be scrollable while keeping the label stationary, as shown in the original Codepen (this detail may not have been clear from my description unless you referenced the Codepen).

The key lesson for me was ensuring each container has a defined height, including the main container.

.main_container {
  height: 100vh; /* occupying full viewport height */
  overflow: hidden; /* preventing scrolling overflow */
}

.notes_scroll {
  height: calc(100% - 25px); /* accounting for 'Notes' label height */
  overflow-y: auto;
}

div{
  border: 1px solid #AAA;
}

.problem-div{
  background-color: #FBB;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<section class="grid">
  <div class="main_container">
    <div class="row h-100">
      <div class="col-sm-4">left</div>
      <div class="col-sm-8">
        <div class="row h-50">
          top
        </div>
        <div class="row h-50">
          
          <div class="col-sm-6 h-100">
            <div class="row">
              Notes
            </div>
            <div class="row problem-div notes_scroll">
              Many notes 1<br>Many notes 2<br>
              Many notes 3<br>Many notes 4<br>
              Many notes 5<br>Many notes 6<br>
              Many notes 7<br>Many notes 8
            </div>
          </div> 
          <div class="col-sm-6">bottom right</div>
        </div>
      </div>
    </div>
    
  </div>
</section>

Link to Codepen

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

IE8 - Unable to use rgba()

I'm currently facing an issue with RGBA() manipulation in jQuery while using IE 8. Here's the code I have so far: $('.set').click(function (e) { var hiddenSection = $('div.hidden'); hiddenSection.fadeIn() . ...

Form Rolling Over

I recently updated the navigation bar on our pending site by incorporating styled hyperlinks. Initially, one of these hyperlinks directed to PayPal, but due to security concerns, I had to replace it with an encrypted button instead. The challenge I' ...

Firefox triggers VideoJS event 'ended' at the beginning

When I use video JS, the "ended" event in Firefox is triggered both at the end and beginning of the video playback. Check out this example in FF: http://jsfiddle.net/planadecu/a3Chu/ All other browsers seem to work fine with this. The code snippet calle ...

What is the best way to keep my bottom navigation bar attached to my top navigation bar?

I am facing an issue with my two navbars. The top navbar sticks to the top of the page when the user scrolls down, which works perfectly fine. However, when the user logs in, the bottom navbar becomes visible. Unfortunately, the bottom navbar is not sticky ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Leveraging webpack for CSS bundling

I'm currently working on bundling some NPM modules using webpack instead of utilizing a CDN. While I've successfully managed to integrate the .js files, I'm facing challenges with including the .css files for these modules. One example is ...

The iPad1 is having trouble displaying the background image, while the iPad2 is showing it perfectly

I am experiencing issues with the background image on my website not displaying properly on an iPad 1, however it appears fine on an iPad 2. Does anyone have any suggestions or ideas on how to fix this? Thank you in advance. ...

Trouble displaying complete image (ReactJS, CSS)

I am looking to create a visually stunning landing page that covers the entire screen, showcasing a captivating image. However, I have encountered an issue where only a portion of the image is visible due to additional divs being created. Here is what my ...

Could anyone assist me in positioning my personalized Context Menu beside my cursor?

If possible, I would appreciate some assistance with my custom context menu. It may require some minor tweaks or a fresh idea on how to address the issue. Ideally, I would like to keep the HTML and CSS somewhat unchanged. [I'm not sure what to write ...

Error message "HTTP500: Server error detected on aspx page is exclusive to Edge browser."

Recently, I've been working on configuring the website to be compatible with various browsers. Initially, the page was tested and debugged on Chrome, IE, Firefox, and Opera without any issues. However, when I tried debugging it on Edge, a problem aro ...

Impact of designs on the elements within components

Here's a CSS query I have: If I have the code below, does the styling apply to the contents of v-container but not v-container itself? <v-app id="inspire"> <v-container justify-center style="max-width: 1200px"> <v-layout row ...

Ways to decrease the height of the navigation bar in Bootstrap version 4.1

Struggling to figure out how to decrease the height of the navbar in bootstrap v4.1. I want it to be specifically 24px tall; Thanks, Richard. ...

What steps should I take to align my header to the topmost section of the browser window?

I'm having an issue with my header background image not lining up exactly to the top of the browser. There is a gap between the header image and the top of the browser, showing the background color. I want the header to extend all the way up to the to ...

Creating a responsive design for embedding YouTube videos in web pages

I am facing an issue with embedding YouTube videos on my webpage. I have tried to make the videos responsive by using the code provided below, which works well on mobile and tablets. However, on desktops and laptops, the videos appear very large. As a work ...

Is there a way to showcase a row of images when a button is clicked?

I am looking to create a functionality where pressing one of the buttons shown in the image below will toggle visibility of specific sections containing 3 images each. For example, clicking on "Tapas" will only display tapas images and hide main course ima ...

Tips for customizing bootstrap cards to display on a single row with four columns

I've been working on my Angular application and I'm using a Bootstrap card to display some information. Unfortunately, I can't seem to get the design to look how I want it to, even after trying multiple approaches. The component.html code ...

Unlocking the power of setting dynamic meta content

When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error: SyntaxError: Unexpected identifier in /home/runner/superstrap/ ...

Shrinking the image within a card row in Laravel/Bootstrap 5

My issue involves creating a row of Bootstrap cards using Laravel. When I add the "row" class, the images in the cards shrink. However, if I remove the "row" class, the images display perfectly. How can I fix this problem? <h1>Vehicles</ ...

Creating a stunning image fade-in effect with CSS3

I have been working on a way to make images fade in using CSS3 once they have finished loading. However, I am facing an issue where the image fades in and out for a brief moment twice instead of just being blank and fading in. My attempt at a solution inv ...

Tips for creating square corner images within a bootstrap card group

How can I create a square image gallery using Bootstrap 4 cards? <div class="card-group flex-wrap"> <div class="card border-dark"> <img class="card-img-top" src="h ...