Scrolling the inner container in a CSS flexbox layout: tips and tricks

I'm currently working on designing a flex box based HTML layout like the one shown in the image link provided below. https://i.stack.imgur.com/nBl6N.png The layout should consist of 3 rows within the viewport with the following structure:
- top: "main header"
- middle: "home"
- bottom: "main footer"

The "home" section should have a left side "menu" and a "container" on the right.

Within the "container" there should be:
- top: "container-header"
- middle: "box container"
- bottom: "container-footer"

The middle "box container" needs to be scrollable.

I've managed to create the layout successfully. You can view the codepen link here.

Here is an excerpt of the code:

HTML, body {
height: 100%;
overflow: hidden;
margin: 0;
}
  
.window {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100%;
}
  
.header {
display: flex;
flex-direction: row;
justify-content: space-between;
}
  
.footer {
display: flex;
flex-direction: row;
justify-content: center;
background-color: cyan;
}
  
.home {
display: flex;
flex-direction: row;
flex: 1;
min-height: 0;
}
  
.menu {
width: 200px;
border: 1px solid green;
flex-shrink: 0;
}

.container {
display: flex;
flex-direction: column;
justify-content: space-between;
overflow: auto;
flex: 1;
border: 1px solid blue;
}

.container-header {
width: 100%;
text-align: center;
background-color: purple;
}

.container-footer {
text-align: center;
background-color: mediumslateblue;
}

.box-container {
display: flex;
justify-content: space-between;
overflow: auto;
border: 1px green solid;
}

.box {
width: 600px;
height: 600px;
margin: 10px;
background-color: red;
text-align: center;
padding: 5px;
flex-shrink: 0;
}
<html>
<head>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="window">
<div class="header">
<div>Left</div>
<div>Right</div>
</div>
<div class="home">
<div class="menu">
menu
</div>
<div class="container">
<div class="container-header">
Container Header
</div>
<div class="box-container">
<div class="box">
box - 1
</div>
<div class="box">
box - 2
</div>
<div class="box">
box - 3
</div>
</div>
<div class="container-footer">
Container Footer
</div>
</div> 
</div>
<div class="footer">
<div>Left</div>
<div>Right</div>
</div>
</div>
</body>
<html>

Despite achieving the desired layout, I've encountered a slight issue.

In order to make the "box container" scrollable, I initially assumed setting the overflow property of the "box container" to auto would suffice.
However, it appears that this alone does not achieve the desired effect. I had to set the overflow property of the "container" to auto as well in order to make the "box container" scrollable.
In fact, setting the overflow property to auto for both the "container" and "box container" is necessary.

Why is this additional step required?

Answer №1

One reason for needing overflow:auto is because the first container is overflowing its own container.

To remedy this issue, you should include min-width:0 in the container so that only its inner content overflows, requiring only overflow:hidden on the box-container.

html,
body {
  height: 100%;
  overflow: hidden;
  margin: 0;
}

.window {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
}

.header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.footer {
  display: flex;
  flex-direction: row;
  justify-content: center;
  background-color: cyan;
}

.home {
  display: flex;
  flex-direction: row;
  flex: 1;
  min-height: 0;
}

.menu {
  width: 200px;
  border: 1px solid green;
  flex-shrink: 0;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width:0;
  flex: 1;
  border: 1px solid blue;
}

.container-header {
  width: 100%;
  text-align: center;
  background-color: purple;
}

.container-footer {
  text-align: center;
  background-color: mediumslateblue;
}

.box-container {
  display: flex;
  justify-content: space-between;
  overflow: auto;
  border: 1px green solid;
}

.box {
  width: 600px;
  height: 600px;
  margin: 10px;
  background-color: red;
  text-align: center;
  padding: 5px;
  flex-shrink: 0;
}
<div class="window">
    <div class="header">
      <div>Left</div>
      <div>Right</div>
    </div>
    <div class="home">
      <div class="menu">
        menu
      </div>
      <div class="container">
        <div class="container-header">
          Container Header
        </div>
        <div class="box-container">
          <div class="box">
            box - 1
          </div>
          <div class="box">
            box - 2
          </div>
          <div class="box">
            box - 3
          </div>
        </div>
        <div class="container-footer">
          Container Footer
        </div>
      </div>
    </div>
    <div class="footer">
      <div>Left</div>
      <div>Right</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

Retrieving an Instance of Google Maps Object with the Help of JQuery

I'm currently working on a script that, when executed, will retrieve an instance of a Google Maps object from the webpage using JQuery. For example, if there is a map present on the page, it will be structured like this: <div class="map">....& ...

XPath allows for the selection of both links and anchors on a webpage

Currently, I am using Screaming Frog and seeking to utilize XPath for a specific task. My objective is to extract all links and anchors that contain a certain class within the main body content, excluding any links found within div.list. I have attempted ...

I am experiencing an issue with Safari where there is an unexpected rounded border appearing that

Upon inspecting the image, a slight border is visible behind the main border on Chrome and Firefox. However, Safari and iPhone display a rounded thin border instead. Can you help us understand why this discrepancy is happening and how we can remove it? P ...

Sharing a codepen.io project on a GitHub page

After completing the courses on free code camp, I successfully created a tribute page project on codepen.io. However, when attempting to display the finished website on GitHub using GitHub pages, I have encountered issues. No matter what steps I take, the ...

What is the process for adding extra CSS to a webpage displayed in WebView2?

Currently, I am utilizing the Webview2 control within a winform application. My goal is to enhance the behavior of the loaded page by injecting additional CSS code when a specific URL is being displayed in the browser control. Specifically, I aim to implem ...

Is there a method to automatically execute an 'onchange' function whenever a value is updated due to an AJAX response?

Many drop down menus are present on my page; <... onchange="callthisfunction(...)"...> While a manual change easily triggers the function, using ajax to load saved data does not register the value as changed and the function is not triggered. Is th ...

When a page is being redirected using requestdispatcher in a filter, the CSS contents do not seem to be applying correctly

Upon evaluation of the condition, if it fails, I am utilizing a request dispatcher to redirect to another page. The redirection is successful, however, the CSS is not being applied to the new page. What could be causing this issue? public class RolePrivil ...

Utilizing different levels of `<div>` within the card-body component in Bootstrap5

While following a web application tutorial on YouTube, I encountered a question. The code snippet is shown below: <div class="card card-body"> ... </div> The resulting output is displayed in this image: https://i.sstatic.net/D3iBL.p ...

Steps for incorporating a storyline into a CSS Chart

I'm attempting to introduce a horizontal line at a specific point using chartscss.org For instance, on the given chart utilizing Charts CSS below, I'm aiming to include a horizontal line at 15.5... Similar to the illustration shown. I've e ...

Attempting to achieve the simultaneous display of an image overlay and image zoom when hovering over the mouse

I'm struggling to display an image overlay and image zoom simultaneously when hovering the mouse. After adding the image zoom effect, the overlay disappears. It seems like one transition is overriding the other. Whenever the zoom works, the overlay do ...

Rotation using CSS in Internet Explorer 8

Can anyone help me with a CSS solution for rotating elements in IE8? I've tried some solutions that claim to work in IE8, but they're not working for me. What am I doing wrong? Here's what I've attempted: <!DOCTYPE html> <htm ...

Input field for postal code containing only numbers (maximum 5 digits) in Angular version 4/5

I am struggling with creating an input field that accepts numbers. If I use type="text", I can only type 5 characters of alphanumeric data, but if I use type="number", it allows any number input without limiting it to just 5 numbers. Thank you in advance f ...

Changing font color of a selected item in Material-UI's textview

I have a select textview in my react app and I am wondering how to change the font color after selecting an item from this textview. <div> <TextField id="standard-select-currency" select fullWidth l ...

In an AJAX response, the button will be disabled if any checkboxes are left unchecked, regardless of their group

This text has been inspired by a thread on Stack Overflow regarding disabling a button based on checkbox selection In the original post, the button is disabled unless at least one checkbox is checked. In my scenario, I have two sets of checkboxes: <d ...

Arrange the menu items in a column layout based on a given condition

Can someone assist me with displaying the menu subitems? I have created a plunker. Take a look at it to understand what I need (open plunker in full screen) https://plnkr.co/edit/IMEJFPfl5kavKvnUYaRy?p=preview In the plunker above, there are two dropdown ...

I am facing issues with utilizing if endif for my Internet Explorer stylesheet in HTML/CSS

<!--[if IE]> <link rel="stylesheet" type="text/css" href="ie.css" /> <![endif]--> <link rel="StyleSheet" href="style.css" /> Whenever I apply this code, the content in Internet Explorer is being influenced by both style.css and ie. ...

When a specific item is selected from a drop-down menu, text boxes and drop-downs will dynamically appear and change

In the current version of my code, there is a single drop-down menu with three options for the user to select from. If "Complete" is chosen, a text box should appear. If "Abandon" or "Transfer" is selected, a separate drop-down menu needs to be displayed. ...

An error occurs when trying to modify the classList, resulting in an Uncaught TypeError for setting an indexed property

I am attempting to modify the classes of multiple sibling elements when a click event occurs. Some of these elements may have multiple classes, but I always want to change the first class. Below is the code that I created: let classList = event.currentTa ...

"Troubleshooting: IE compatibility issue with jQuery's .each and .children functions

I have created an Instagram image slider feed that works well in Chrome, Safari, Firefox, and Opera. However, it fails to function in all versions of Internet Explorer without showing any error messages. To accurately determine the height of images for th ...

Click a button to adjust the height of an element

My webpage features a dashboard with tabs on the left and corresponding content on the right. To ensure proper alignment, I have utilized Bootstrap to create two columns - one for the tabs and one for the content. However, I am facing an issue where the bo ...