Flexbox causing overflow in parent container due to max-height set to 100%

In my HTML code, I have the following elements:

  1. .modal - represents a page modal with a height limit of 30vh.

  2. .wrapper - serves as a container that wraps the modal content, consisting of an input (in this case, a search input) and a list of items.

  3. .list - contains a list of elements that are wider than 30vh.

The desired behavior is for the wrapper to be filled by the input field, with the remaining space filled by the list. If the number of items in the list exceeds the available space, a scroll should be applied.

I encountered an issue where the list (and .wrapper) overflows the page unless I set display: flex on .modal.

Could someone please explain why this happens? Why does the .wrapper overflow its parent even though it has overflow: hidden and max-height: 100% which equals 30vh of the parent until .modal has display: flex?

.modal {
  background-color: red;
  padding: 3rem;
  flex-direction: column;
  gap: 1rem;
  max-height: 30vh;
}

.item {
  height: 30px;
  background-color: green;
}

.wrapper {
  max-height: 100%;
  display: flex;
  flex-direction: column;
  background-color: orange;
  overflow: hidden;
}

.list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1rem;
  overflow: scroll;
}
<div class="modal">
  <div class="wrapper">
    <input />

    <div class="list">
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
      <div class="item">
        sds
      </div>
    </div>
  </div>
</div>

Answer №1

To enhance the modal, include the display flex property or restrict the height of the wrapper using calc(30vh - 6 rem)

.modal {
  background-color: red;
  padding: 3rem;
  flex-direction: column;
  gap: 1rem;
  max-height: 30vh;
  overflow: hidden;
  display: flex;
}
.wrapper {
  max-height: 100%;
  display: flex;
  flex-direction: column;
  background-color: orange;
  overflow: hidden;
  max-height: 30vh;
  height: 100%;
}
.list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1rem;
  overflow: scroll;
}
.item {
  height: 30px;
  background-color: green;
}

Answer №2

To enhance the design, simply add one line of CSS and delete another.

overflow-y: auto should be applied to .modal

as well as

remove overflow: scroll; from .list

.modal {
  background-color: red;
  padding: 3rem;
  flex-direction: column;
  gap: 1rem;
  max-height: 30vh;
  overflow-y: auto
}

.item {
  height: 30px;
  background-color: green;
}

.wrapper {
  max-height: 100%;
  display: flex;
  flex-direction: column;
  background-color: orange;
  overflow: hidden;
}

.list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1rem;
}

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

Identify when a user exits the webpage

Important! Please review this question before marking it as a duplicate I am looking for a solution that will automatically redirect a visitor to another page when they leave my webpage. The goal is to keep the page open and engage the user even if they s ...

Can you please tell me the CSS pseudo element that corresponds to the AM/PM field in an <input type="time">

The issue with the am/pm display is dependent on the browser that the app is opened in. I am looking for a universal solution to ensure it does not appear in any popular browsers. Below is the HTML code for my input time field: <input type="time" for ...

Superior Option for Displaying Advertisements Instead of <aside>

My excitement peaked when I stumbled upon the following information on Mozilla Publisher Network: In HTML4, every section is part of the document outline. However, documents are not always linear. There may be special sections within a document that con ...

Translucent" outline for an "opaque" object

Consider the following HTML snippet: <div id="my_div"> </div> Now, let's take a look at the CSS code: #my_div { width: 100px; height: 100px; background-color: #0f0; op ...

CSS - text featuring a gentle shadow hue and a deeper foreground color

I'm encountering an aesthetic issue where my list of texts have random light/bright colors, making them hard to read on white backgrounds. I've tried setting the text color to shadow, but now I want the text itself to be a darker, more readable c ...

Using Next.js in conjunction with Tailwind CSS and the shadcn/ui library to tackle the issue of preventing overscroll from creating

As I delve into a Next.js project, my goal is to use Tailwind CSS to craft an interface featuring a sidebar on the left, a header at the top, and a main content area. However, an issue has surfaced where users can over-scroll, leading to a blank space appe ...

Guide on integrating animate.css animations with Vue's Transition and TransitionGroup components

Vue offers the v-if and v-for directives that allow you to manipulate elements in the DOM based on certain conditions. In order to animate elements controlled by v-if and v-for, you need to utilize the built-in Transition and TransitionGroup components. Bu ...

Having a peculiar margin problem in IE11 - potential connection between overflow and smooth scrolling

Lately, I've been facing a small issue with a website plenarto.github.io that has been bothering me for the past few days. There is an odd white margin that mysteriously appears on the right side of the website whenever you access it using IE11. This ...

What is the best method for eliminating the border of an Angular mat-form-field when it is in a disabled state?

I've been attempting to eliminate borders from a mat-form-field when the field is disabled, but despite trying various solutions found online, I haven't been successful in removing the borders from the div. Below is the code snippet: <div ...

Tips for highlighting text in HTML without using a specific tag

My current project involves customizing a Wordpress plugin, but I've encountered a frustrating obstacle. I'm having trouble removing the three dots that appear at the bottom of the download card. Despite my efforts, I can't seem to find any ...

Which client architecture is most suitable for an LCD wall application?

My task is to create a large LCD wall application similar to the one featured in this advertisement. The requirements include the ability for the wall to display full-screen videos or divide the screen into sections to show multiple videos or images. I a ...

What is the best method for creating thumbnail URLs for video files in HTML with JavaScript?

I am facing an issue with generating a thumburl for uploaded video files. After dynamically creating an input file and uploading local video files, I am able to retrieve the name and size of the file along with other information as shown in the screenshot ...

How to Save Styles as a CSS File in Angular 4

I'm a beginner in Angular 4 and I'm trying to export my styles as a css file. Whenever I serve or build the project, it generates a style.bundle.js file and includes the css as an html style element. However, I would like to export it as a style ...

The hamburger menu in Bootstrap 5 is not displaying properly on mobile screens and appears to be unstable

Although the desktop screen is working fine, I am encountering 2 issues on the mobile screen: Check out this JSFiddle Demo: https://jsfiddle.net/uyvdqL9s/ On the mobile screen, the hamburger menu appears in the center instead of on the right along with t ...

The template in AngularJS route fails to display

I am currently facing an issue with loading multiple pages using AngularJS $routeProvider and two controllers. Whenever I click on a link from the first partial to go to the second one, the second partial fails to render properly, displaying only template ...

Different choices for pick component in Bootstrap

Struggling to create an inline label with a select element using Bootstrap, I came across this helpful example on jsfiddle: http://jsfiddle.net/kokilajs/1q53pr6j/ <form id="searchForm" method="get" class="form-horizontal" role="form"> <div class= ...

Issue with input-group background display in bootstrap 4

My new computer is running on Windows 11 with Chrome version 97. There seems to be a bug in the original bootstrap code, as evidenced by the attachment. The issue can be seen on the Bootstrap website By adding "border-radius:0!important" to ".input-group ...

To seamlessly integrate a variable into the <img src='VARIABLE'> tag, follow these steps

When inserting a dictionary value directly into the <img src tag, the image displays properly. However, using the variable 'musician' (which holds a URL) results in an empty frame with a broken image logo. musician_dict = { 'g': ...

Save a PNG file using the HTML5 Canvas when the Submit button is clicked, with the help of PHP

I am looking for assistance with a signature capture form. I came across a standard template on Github that works perfectly, but I wanted to customize it further. However, my Javascript skills are still in the early stages. The current code allows you to c ...

Unable to remove Css class through code behind

I'm currently working on a Gridview code snippet: <SelectedRowStyle CssClass="SelectedRowStyle" /> This changes the color of the selected row to yellow. However, when I click a button, I want to remove the css class. I attempted to do this in ...