What is the correct way to implement overflow-y: auto for vertical flex-items?

Check out this interactive example. Here is the HTML code:

<div class="overflow">
  <div class="block">
    <div class="header">
      This is a green header
    </div>
    <div class="content">
      This is a lot of content<br> this is a lot of content<br> this is a lot of content<br> this is a lot of content<br> this is a lot of content<br> this is a lot of content<br>
      <button>
      add more
      </button>
    </div>
  </div>
</div>

And here is the corresponding CSS code:

.overflow {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0,0,0,.8);
}
.block {
  width: 100px;
  background: white;
  max-width: calc(100% - 30px);
  max-height: calc(100% - 30px);
}
.header {
  background: green;
  flex: 0 0 auto;
}
.content {
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
}

The issue is that when the content exceeds the modal boundaries, instead of adding a scrollbar to the content, it overflows the .content div.

I attempted to resolve this by using min-height:0;, but it did not solve the problem.

Answer №1

Check out this CSS snippet for creating an overflow container:

.overflow{
  position: fixed;
  width: 100%;
  height: 100vh;
  overflow-y: scroll;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0,0,0,.8);
}
.block{
  width: 100px;
  background: white;
  max-width: calc(100% - 30px);
  max-height: calc(100% - 30px);
}
.header{
  background: green;
  flex: 0 0 auto;
}
.content{
  min-height: 0;
  overflow-y: auto;
  text-overflow: clip;
  flex: 1 1 auto;
  background: #fff;
}

See it in action here: https://jsfiddle.net/example_user/w9ryv2Ln/2/

Answer №2

Big thanks to everyone who took part in solving this issue, your contributions really helped me identify the problem. I had completely overlooked adding

display: flex; flex-direction: column
to the .block element.

Behold, the corrected code:

.overflow{
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0,0,0,.8);
}
.block{
  display: flex;
  flex-direction: column;
  width: 100px;
  background: white;
  max-width: calc(100% - 30px);
  max-height: calc(100% - 30px);
}
.header{
  background: green;
  flex: 0 0 auto;
  height: 50px;
}
.content{
  //min-height: 0;
  overflow-y: auto;
  text-overflow: clip;
  flex: 1 1 auto;
}

Answer №3

$(document).ready(function(){
$('button').click(function(){
  $('.content').append('some more content<br>');
  });
});
.overflow{
     position: absolute;
     width: 100%;
     height: 100%;
     display: flex;
     justify-content: center;
     align-items: center;
     background: rgba(0,0,0,.8);
    }
    .block{
     width: 100px;
     background: white;
     max-width: calc(100% - 30px);
 
    }
    .header{
     background: green;
     flex: 0 0 auto;
    }
    .content{
     float:  left;
     clear: left;
     overflow-y: auto;
     max-height: 350px;
   
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="overflow">
  <div class="block">
    <div class="header">
      This is a green header
    </div>
    <div class="content">
      This is a lot of content<br> this is a lot of content<br> this is a lot of content<br> this is a lot of content<br> 
      <button>
      add more
      </button>
    </div>
  </div>
</div>

After some time, I was able to figure out a solution. Try giving maximum height to the content as provided here. Hopefully, this will help. Give it a try and let me know.

It will become scrollable after reaching a maximum height of 350px. You can adjust this value to suit your needs.

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

How can I implement a recursive nested template call in Angular 2?

Hopefully the title isn't too misleading, but here's my dilemma: I am in the process of building an Angular 2 app and utilizing nested templates in multiple instances. The problem I am facing involves "widgets" within my app that can contain oth ...

Place a gap at a specific spot within the boundary line

Here is a CSS code snippet that displays a horizontal line: .horizontalLineBottom { border-bottom:solid #6E6A6B; border-width:1px; } Is it possible to add space at a specific position on this line? For example, _________________________________ ...

The website functions properly with Live Server, but encounters issues when launched from the Files directory

After testing multiple browsers, the site consistently works fine with Live Server. However, when accessed through Firefox or Chrome, my style.css fails to load completely. On Edge, an error message indicates that the file is missing. Below are snippets of ...

Leveraging this jQuery code across multiple div elements

As a newcomer to jQuery, I am currently experimenting with a test script. My goal is to create an effect where when an image is clicked, a div slides down displaying more information similar to the new google images effect. The issue with the current scri ...

The information is not appearing in the dropdown menu

The select tag for chapters is not displaying the result from the query properly. Instead of showing in the select tag, the chapter names are being displayed as echo statements. <!doctype html> <html> <head> <meta charset="utf-8"> ...

"Utilizing Bootstrap's form-check feature places the checkbox alongside the text

While attempting to create a checkbox using the Bootstrap form-check class, I encountered an issue with my code. Here is the snippet: <form> <div class="form-section"> <div>Title</div> <div class="form-check"> ...

Creating dependent dropdown lists is a useful way to streamline data entry and ensure accuracy in your

I am looking to create a series of 4 connected dropdown lists, structured like this: District: <select id="district"> <option>Select a District</option> <option value="district1">dstrict1</optio ...

Mobile FPS suffering due to slide-out drawer performance issues

My application features a drawer menu that functions smoothly on desktop, but experiences issues on mobile devices with noticeable lag. Within the header, I have implemented a boolean value that toggles between true and false upon clicking the hamburger i ...

The left and right edges are not extending across the entire screen

Can anyone help me determine if this is a duplicate issue? I'm unsure why my left and right borders are not extending the full width of the screen. The website in question is ojs.monojitbanerjee.co.cc Here is the structure: <body> <div id= ...

Leveraging AJAX to access a PHP file

I'm currently attempting to access a PHP file that updates a database entry. To ensure that only specific parts of my webpage are updated, I intend to utilize AJAX. Once the PHP file has executed, I aim to modify an image on the triggering page. Coul ...

Eliminate the preset padding on the left and right sides of the container-fluid in Bootstrap 5

How can I remove the default padding-left and padding-right in a container-fluid using Bootstrap 5? I don't want to use "!important" in my CSS file. I have already tried disabling the default padding with Chrome dev tools but it didn't work. Any ...

The div block containing the table is experiencing horizontal overlap as additional elements are inserted

When creating a table within the div section of the code, I am incorporating text using the DRAG and DROP feature with appropriate styling. The table adjusts in size when I resize my window, indicating that it is functioning correctly. However, as the num ...

What is the best way to prevent autoplay on a local video embedded in an iframe within a

I have the following code in an HTML5 web page, using div and iframe. Whenever I load the page, video1.mp4 starts automatically. :( I do not want video1.mp4 to start automatically. Can anyone help me with this issue? Thank you very much. ...

I'm struggling to find a way to turn off the scroll bar

I'm having trouble disabling the scroll bar on my website for specific reasons. Can anyone provide me with some guidance? <iframe src="video" width="960"height="540" frameborder=0 ></iframe> ...

Footer suffering from image invisibility syndrome

Having trouble displaying two social media images in my footer. Despite including them in my image map correctly, they just won't show up on the website. I've tried a lot of different things, but nothing seems to be working. Could it have somethi ...

tool for showcasing data on a webpage with a specific layout

Which chart library is best suited for presenting data in the formats shown in the images below? Can HighCharts handle these formats? Does Google Charts allow for a combination of bubbles and lines? ...

Webfont issues can lead to incorrect display on your website

Hello fellow Stackers! I could really use some help with a few IE-specific bugs I've encountered while working on a website for a friend. Check out the Website Here | View the Full CSS File The issue I'm facing is with the Google Webfont Ralewa ...

What is the best method to showcase an array representing a key-value pair enclosed in {} as a list item within VueJS?

I have a specific object structure with a key that contains an array as its value. How can I present this information to the user in a list format? allComponents: [ {name: 'Standard field', uses: ['Inconsistent inputs', 'Formul ...

Tips for transferring the asp:Label ID from an asp:CheckBox

I have searched various online forums without success in finding a solution to my issue. I am looking for a way to retrieve an asp:Label's id and pass it to a JavaScript function using the "onclick" event from a nested asp:CheckBox. My goal is to pas ...

Trouble encountered when trying to use an anchor link with the .slideUp jQuery function

Would you be able to assist me with understanding why my anchor links are not functioning correctly? I have 3 anchor links, for example: Google (not working) Yahoo (not working) Facebook (working) Any insights on why Google and Yahoo are not working? &l ...