When the flexbox is nested in a container with a flex-direction of column, the scrolling

I'm facing a challenge in setting up a basic message container that can scroll when necessary. The issue arises when I place the message box within another container and add a side bar, causing the message box to exceed the parent container's height due to an overflow setting problem. How can I achieve a scrolling message box while having a column adjacent to it?

.flex-container {
    display: flex;
}
.flex-fill {
    flex: 1 0 auto;
}
.flex-rows {
    flex-direction: column;
}
.message-container {
    justify-content: flex-end;
    overflow-y: auto;
    flex: 1;
}
.message {
    height: 3rem;
}
<body>
    <div class="flex-container flex-fill flex-rows" style="background-color: red; padding: 1rem; height: 500px;">
        <div id="top-bar" style="background-color: purple;">
            <h3>Working Scroll</h3>
        </div>
        <div class="message-container" style="background-color: white; padding: 1rem;">
            <div class="message">Testing Message</div>
... (Remaining code continues as before) ...

Explore a live demo on CodePen. https://codepen.io/dwpuush/pen/eYzvQQJ

Answer №1

Make adjustments to the flex-fill class so that the element can shrink, and also include min-height:0; to ensure the element shrinks beyond its content size when necessary. For detailed information on this, refer to this discussion.

.flex-container {
    display: flex;
}
.flex-fill {
    flex: 1 1 auto; /* replacing 1 0 auto*/
    min-height: 0;
}
.flex-rows {
    flex-direction: column;
}
.message-container {
    justify-content: flex-end;
    overflow-y: auto;
    flex: 1;
}
.message {
    height: 3rem;
}
<body>
    <div class="flex-container flex-fill flex-rows" style="background-color: red; padding: 1rem; height: 500px;">
        <div id="top-bar" style="background-color: purple;">
            <h3>Working Scroll</h3>
        </div>
        <div class="message-container" style="background-color: white; padding: 1rem;">
            <div class="message">Testing Message</div>
            <div class="message">Testing Message</div>
            <div class="message">Testing Message</div>
           <!-- remaining message elements -->
        </div>
    </div>
    <div class="flex-container flex-fill flex-rows" style="background-color: green; padding: 1rem; height: 500px;">
        <div id="top-bar" style="background-color: purple;">
            <h3>Broken Scroll</h3>
        </div>
        <div class="flex-container flex-fill" style="background-color: blue; padding: 1rem;">
            <div class="message-container" style="background-color: white; padding: 1rem;">
                <div class="message">Testing Message</div>
                <div class="message">Testing Message</div>
                <!-- remaining message elements -->
            </div>
            <div id="right-bar" style="background-color: orange; width: 50px;">
                <h3>Right</h3>
            </div>
        </div>
    </div>
</body>

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

What is the best way to prevent images from being loaded with broken links?

Currently, I am working on a new feature that involves rendering images from servers. In the process of aligning these images, I noticed an excessive amount of white space due to loading images with broken links. https://i.stack.imgur.com/jO8BR.png Here ...

Issues with the use of overflow:hidden

I need to create 5 divs aligned horizontally next to each other, spanning the width and height of the window. Each div should occupy 20% of the window width, and any overflow content should be hidden. Below is the CSS code for two of the divs: #container1 ...

What is the proper way to incorporate html entities in my specific situation?

In my application, I am attempting to incorporate double macron characters. My current method involves using the &#862; entity, like this: t&#862;t, which results in t͞t. However, I have encountered issues with reliability, as sometimes the ...

Guide on making a color legend with JavaScript hover effects

On my website, there is a dynamic element that displays a table when values are present and hides it when there are no values. The values in the table are color-coded to represent different definitions. I wanted to add hover text with a color key for these ...

Are you experiencing issues with Font Awesome Fonts failing to load?

I am currently using a rails app with bower to manage my assets, avoiding gems for JS and CSS. In the directory vender/assets/stylesheets, I have "font-awesome" containing both scss and fonts. In app/assets/stylesheets/application.scss, I have included va ...

I encountered an AJAX error while working on my project

Encountering an error with my login_process.php when using ajax, preventing me from progressing. All files are stored in a single folder. Upon running the HTML log-in page, the error becomes visible. Below is the code snippet, quite straightforward. Previo ...

Can you tell me why the jquery datepicker control is only loading a portion of the CSS?

Working on a new C#/ASP.Net application and I decided to incorporate the datepicker control that I've used before. I transferred all the necessary components from my previous app to this one. The appearance of the datepicker in the old app was like th ...

What's the best way to implement two AJAX field validations to prevent button redirection on a website?

Can anyone provide guidance on how to implement two ajax field validations for a button to prevent clicking if the data already exists or redirecting to the same page? I am seeking assistance with this issue. Below is the ajax code snippet: function vali ...

Organize items within a compartment using Bootstrap3

I have decided to update my website to Bootstrap3 and encountered a common issue that many others seem to be facing as well. I am trying to evenly distribute 4 elements, each approximately 220px wide, inside a 990px container. In the old version of Bootstr ...

Caution: Additional server attributes detected: style

Alert in my Next.js project, I encountered the following message: Notice: Server is sending additional attributes: style I am uncertain about the source of this warning and therefore unable to provide any code snippet. ...

Angular: Display an element above and in relation to a button

Before I dive in, let me just mention that I have searched extensively for a solution to my problem without much luck. The issue is describing exactly what I'm trying to accomplish in a way that yields relevant search results. If you're interest ...

Having trouble with loading images in Bootstrap 5 framework

I am currently developing a website using HTML in Visual Studio Code with Bootstrap 5. However, I am facing an issue where the images I have inserted into a new section are not loading, while the ones in previous sections are displaying perfectly fine. < ...

How can the selected value be shown in the dropdown menu after moving to a different webpage in HTML?

My application features 4 roles displayed in a dropdown menu. When a specific role is clicked, it should go to the corresponding href link that was specified. However, I encountered an issue where after navigating to the second HTML page, the selected rol ...

What is the best way to create a layout for Flexbox project boxes with three boxes per row?

My project cards are displaying in a single horizontal line and expanding infinitely to the right, rather than appearing in rows of three as intended. This causes them to extend outside the visible area in a long horizontal strip. See the issue in action ...

Development of an Angular 4 application utilizing a bespoke HTML theme

I'm in the process of creating an Angular 4 project using Angular CLI and I need to incorporate a custom HTML theme. The theme includes CSS files, JS files, and font files. Where should I place all of these files? Should they go in the asset folder? O ...

Trouble displaying image due to issues with javascript, html, Angular, and the IMDb API integration

I have been working on displaying images from the IMDb API in my project. Everything works perfectly fine when I test it locally, but once I deploy the project to a server, the images do not load initially. Strangely, if I open the same image in a new tab ...

Can additional classes/styles be added to a CSS3 background?

Today I discovered that CSS3 has the capability to support multiple backgrounds, which is truly amazing. What I would love is the option to combine multiple backgrounds dynamically, like this: .Watermarked{ background: url('text/logo.png') bot ...

Spacing between hidden checkboxes and their labels

I've customized some checkboxes to resemble a select dropdown by hiding the input and using only the label as the visible element. However, I'm struggling to add margin between the elements, which seems to work only when the checkbox is visible. ...

I possess a primary menu with several submenus, yet I am encountering difficulty accessing the submenus. My goal is to efficiently navigate and access the appropriate submenu within the main menu

I am facing an issue with my CSS where the sub menu is currently showing from the left side, but I would like it to slide up and down instead. .outer { width: 100%; text-align: center; background-color: Gray; padding-top: 20px; bord ...

Show dynamic HTML Dropdowns with nested JSON data

I've been racking my brains trying to implement this specific functionality in the UI using a combination of HTML5, Bootstrap, CSS, and JavaScript. My goal is to create dropdown menus in the UI by parsing JSON input data. Please Note: The keys withi ...