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

Ensuring Input Integrity: Utilizing HTML and Javascript to Block Unfilled Form Submissions

Currently, I am working on developing a registration page using HTML and JavaScript. Although I have added the 'required' tag in HTML to prevent users from submitting empty input fields, I noticed that users can bypass this restriction by simply ...

Leveraging an external Typescript function within Angular's HTML markup

I have a TypeScript utility class called myUtils.ts in the following format: export class MyUtils { static doSomething(input: string) { // perform some action } } To utilize this method in my component's HTML, I have imported the class into m ...

Code breaking due to Selenium locating element by class

When my application opens a login page, it looks for valid combinations. Initially, there is a button labeled as "check availability" that Selenium can locate and click on. If the username is already taken, a new button appears with the text "check anothe ...

An exception known as NullPointerException arises when attempting to invoke JDBC within a servlet

While running my RegistrationServlet, I encountered an issue when trying to create a Database object. Below is the servlet code snippet: @WebServlet("/register") public class RegistrationServlet extends HttpServlet { private static final long serial ...

changing pictures with jquery

Struggling with this code snippet: $('#clicked_package').css({"background-image" : "url(img/head_2.png)"}).fadeOut("slow"); $('#clicked_package').css({"background-image" : "url(img/head_2_normal.png)"}).fadeIn("slow"); No matter which ...

Animation controlled by a button

I'm working on a project that involves creating a cartoon version of a record player. I want to incorporate an animation where the play button on the side toggles the spinning motion of the record using webkit, while also starting the audio track. The ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS. Grid: Here's an example o ...

Techniques for Adding Background Color to Fieldset Border

Recently starting to learn HTML and stumbled upon a question: How can I create a background color or image for a field set border? Can I simply use regular color values, or do I need special codes for creating a background color in a field set? Any insig ...

What is the best way to update the text on buttons once they've been clicked for a variety of buttons

I'm encountering an issue with multiple buttons where clicking on any button causes the text to change on the first button, rather than the one I actually clicked on. All buttons have the same id, and using different ids for each button seems impracti ...

The hover effect seems to be disabled in the third-level submenu

I need help creating a dropdown menu that shows an image when hovering over a specific item. I have checked my CSS code and HTML structure, but the image is not appearing as expected when I hover over the "Afyon White" list item. Any ideas on how to fix th ...

Word.js alternative for document files

I'm on the lookout for a JavaScript library that can handle Word Documents (.doc and .docx) like pdf.js. Any recommendations? UPDATE: Just discovered an intriguing library called DOCX.js, but I'm in search of something with a bit more sophistic ...

I'm having trouble with Material Design Slide Toggle as it lacks event.StopPropagation functionality. Any suggestions on what alternative I

When working with the Slide Toggle in Material Design, I noticed that it does not have a stopPropagation event. This is because the "MdSlideToggle.prototype._onChangeEvent" already includes a call to stopPropagation. So, what should be used instead? <m ...

Guide to using two UI grids simultaneously with the directives ng-hide and ng-grid

As a newcomer to AngularJS, I am attempting to create a page with two ui-grid elements and a button labeled "other-grid". The goal is for the first grid to load initially, and when clicked again, it should be replaced by the second grid. However, I am en ...

The Materialize CSS tabs are aligning vertically below each other, but functioning correctly upon refreshing the page

When using materialize css tabs, all the divs load one below the other on the initial page load. If I refresh the page, it starts behaving properly. <div class="row"> <div class="col s12"> <ul class="tabs"> <li class="tab col s ...

Is there a way to generate unique authentication numbers daily without the need to manually adjust any code

I am building a web application for commuters using vuejs and firebase. My goal is to implement the following feature: The employer provides the employee with a unique authentication code daily, which the employee (user) must enter when clicking the "go t ...

"Efficiently handle multiple instances of the same name using AJAX, JavaScript

I have a PHP-generated multiple form with HTML inputs containing IDs and NAMEs. I am struggling to capture all this information. When I click the "Done" button, everything is marked as completed due to the button names. <?$command = "SELECT * FROM exam ...

Customizing background size for iOS devices

This morning, I delved into research on a particular issue. Currently, I am crafting a single-page website that heavily relies on images. While Safari is usually criticized for its quirky handling of background-attachment:fixed, it seems to be working fine ...

Utilizing the optimal method for aligning text to either the right or left side

I am currently working on creating a container element with multiple child elements. I would like these elements to be aligned differently, some left-aligned and others right-aligned. This includes scenarios where the child elements themselves are containe ...

Can you tell me the proper term for the element that allows CSS properties to be changed after a link has been clicked?

I have integrated a horizontal scrolling date selector on my website, and it is functioning well. However, I am facing an issue while trying to change the CSS properties of a clicked date link using jQuery. Despite successfully firing the click event, I am ...