Instructions on how to include a scrollbar within an element nested inside another element with a scrollbar

I have a modal that already has a scroll if the general content exceeds the height of the modal. However, I now need to add a div inside the modal with its own scroll bar. The challenge is that I do not know the exact height of my modal as it is based on 30vh.

If I could simply set a fixed height for the inner div, it would be much simpler, but unfortunately that's not an option.

The problem at hand: I require a separate scroll bar for the right area within the modal.

.container {
  max-height: 30vh;
  border: 1px solid black;
  overflow-x: hidden;
  overflow-y: auto;
}

.header {
  border: 1px solid red;
}

.body {
  display: flex;
}

.leftArea {
  border: 1px solid green;
  width: 50%;
}

.rightArea {
  border: 1px solid blue;
  width: 50%;
  overflow-x: hidden;
  overflow-y: auto;
  /* height: ???; */
}
<div class="container">
  <div class="header">
    HEADER
  </div>
  <div class="body">
    <div class="leftArea">
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
    </div>
    <div class="rightArea">
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
    </div>
  </div>
</div>

Check out my example on CodePen.

Answer №1

When working with block layout, it's essential to have fixed heights for the overflow property to function correctly.

The clarification can be found on MDN:

To make overflow effective, the block-level container should have a defined height (height or max-height) or specify white-space as nowrap.

In contrast, dealing with flex layout offers more flexibility. A container with variable height can still trigger an overflow condition.

To achieve this, simply switch the primary container from its default display: block to display: flex.

.container {
  max-height: 30vh;
  border: 1px solid black;
  overflow-x: hidden;
  overflow-y: auto;
  /* new */ display: flex;
  /* new */ flex-direction: column;
}

.header {
  border: 1px solid red;
}

.body {
  display: flex;
  /* new */ min-height: 0; /* see note below */
}

.leftArea {
  border: 1px solid green;
  width: 50%;
}

.rightArea {
  border: 1px solid blue;
  width: 50%;
  overflow: auto;
}
<div class="container">
  <div class="header">HEADER</div>
  <div class="body">
    <div class="leftArea">
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
    </div>
    <div class="rightArea">
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
    </div>
  </div>
</div>

Take a look at the interactive demo on jsFiddle

Note: To ensure compatibility with most browsers, you must override the default min-height: auto setting. The reason behind this adjustment is discussed here: Understanding why flex items don't shrink beyond content size

Answer №2

Apologies, I may not have fully understood the question.

Does this align with what you were looking for?

.container {
  max-height: 70vh;
  border: 1px solid black;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.header {
  border: 1px solid red;
}

.body {
  display: flex;
  flex-size: 1;
}

.leftArea {
  border: 1px solid green;
  width: 50%;
}

.rightArea {
  border: 1px solid blue;
  width: 50%;
  overflow-x: hidden;
  overflow-y: auto;
  /* height: ???; */
}
<div class="container">
  <div class="header">
    HEADER
  </div>
  <div class="body">
    <div class="leftArea">
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
      <p>left area</p>
    </div>
    <div class="rightArea">
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
      <p>right area</p>
    </div>
  </div>
</div>

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

I am trying to update the content in ckeditor when a different option is selected, but for some reason it is not working

jquery issue: the jquery that I added and the content doesn't change even though the "alert(model);" works fine <script> $(document).ready(function() { $("select.modele").change(function() { var modele = $(this) ...

Having trouble getting CSS absolute and relative positioning to work with my div slider carousel

I'm currently working on creating a slider carousel using VUE, where the CSS classes (3 divs) are looped through. However, I am facing an issue where every time a div fades out, the next slider creates a duplicate slider at the bottom, resulting in tw ...

I implement a Bootstrap navbar on my website, and when I scroll up, the content blocks seamlessly appear behind the fixed navbar at the top of the page

When scrolling up with my Bootstrap navbar, the block content appears behind the fixed top navbar. My Navbar code is as follows: <main id="navfix" class="m-5"> <nav class="navabar_bgc py-0 navbar navbar-expand navbar-l ...

How can we make the Facebook like button display the fan count as the counter?

Can the Facebook like button be customized to display the fan count as the counter, and also update the fan count when clicked (like/unlike)? I've searched through forums and tutorials but haven't found a solution yet. It appears that the standar ...

Encase a group of div elements with identical styling

Looking for a solution on how to programmatically wrap a set of divs with a parent div based on a certain class match. Any suggestions or ideas on how to achieve this? ...

What is the best way to supersede an inline CSS rule with an external file?

Is there a method to replace an inline CSS rule with an external stylesheet file? Review my HTML code: <div class="mydiv" style="background:#000"> lorem ipsom</div> I aim to modify the background color using CSS. Here is my CSS code: .mydiv ...

gulp failed to compile the Sass file

I recently made the switch from using less to sass in my project. I went ahead and installed gulp-sass using npm and updated my gulpfile to compile sass instead of less. However, despite making these changes, gulp is not compiling my .scss file to css. I&a ...

What is the method for assigning a maximum value of 300 to a bootstrap progress bar?

I'm in the process of creating a progress bar with a maximum value of 300, but I'm struggling to set it at 300. It currently works for a maximum value of 100, but I want it to be 300. Here's what I've attempted: <div class="mt-3 pr ...

Struggling to get the font-weights of the Typography and Button components in the new React with Material-UI to display

In both my previous and most recent React applications, I have the following code snippets: <Typography variant="h6" color="inherit" noWrap > h6. Heading </Typography> <Button type="button" size="large" color="primary" > ...

What is the best strategy to prevent reflow and flickering when loading images on a website?

My website displays an image on the left with text on the right, but when the server is busy or the connection is slow, the page flickers. It initially shows the text on the left and then suddenly moves it to the right once the image loads. This issue see ...

The background image on my screen seems to be malfunctioning and not displaying properly

I'm facing an issue with setting an image as the background of the banner section on my website. Despite trying various solutions and researching similar problems, I haven't been able to resolve it. Specifically, I am attempting to set the backgr ...

Slide the toggle icon to activate the add-to-cart button on click!

Is it possible to achieve the functionality of sliding a plus icon to an add to cart button on click, instead of hover? I have attempted using CSS without success. Would JQuery be able to accomplish this task? codepen.io HTML <div class="socialIcons" ...

When attempting to use `$('.classitem').submit(function(event){event.preventDefault})`, the function fails to execute

I have a question I'm eager to find an answer to. Here is the code snippet I'm working with: $('.comment').submit(function(event) { event.preventDefault() Every time I try to submit it, the page reloads instead of submitting th ...

Why are the links in the navgoco slide menu failing to function properly?

I utilized a demo from jQueryRain to create a collapsible menu using jQuery. However, after completion, I discovered that none of the links were functioning properly. Upon visiting the documentation page, I noticed that many users were encountering the sam ...

What steps should be taken to create this specific layout using Vue-Bootstrap?

Currently tackling a web project and aiming to design two rows that resemble the following layout: https://i.sstatic.net/tLv1h.png Details of the screenshot (such as icons) are not necessary, but rather focusing on the arrangement of aligning two rows bes ...

javascript get the value of the text field

Is there a way to calculate even and odd numbers using a text field for entering the range of values and a select tag to choose between even and odd? How can I retrieve the value from the text field in order to pass it to the calculation function? Custom ...

Dynamic mouse parallax effect enhances the experience of a full-screen responsive background

I am looking to create a background that covers the entire viewport, similar to the example shown here: https://css-tricks.com/perfect-full-page-background-image/ Additionally, I want to have 5 images stacked on top of each other (essentially 5 layers of ...

What is the best way to distribute javascript and html code across multiple elements on a webpage?

My webpage has a unique feature - two textboxes that trigger a bootstrap modal with a searchable treeview when clicked. The selected item from the treeview automatically populates the textbox. It's a flawless process! Recently, I decided to implement ...

Issue with Django JQuery datatable ajax refresh: sorting buttons not displaying and function calls not working

Currently, I am utilizing Django along with JQuery and jquery-datatables-bs3 to generate a table for my models. The functionality allows users to add or remove table rows, which in turn creates or deletes model instances within the database. Each row featu ...

Table-driven forms in Angular

I am facing a requirement where I need to submit a form containing five records in a table format. Here is how the table looks: https://i.sstatic.net/82ZFM.png This is the code snippet for the form: <form [formGroup]="FeedBack" (ngSubmit)=&q ...