Navigate through complex layers of flexible containers with scrolling

In this code example, I have created a scrollable container with a flexible nesting structure.

https://jsfiddle.net/n3vctjr4/

.container {
  width: 300px;
  height: 500px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
}

.static {
  padding: 1rem;
  background: red;
}

.flexible {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.flexible div {
  display:flex;
  flex-direction: column;
  min-height: 0;
}

.overflow {
  overflow: auto;
}
<!-- Container element -->
<div class="container">

 <!-- Non-flexible container -->
  <div class="static">HEADER</div>

  <!-- Flexible container -->
  <div class="flexible">
    
    <!-- Nested containers -->
    <div class="nesting-1">
      <div class="nesting-2">
        <div class="nesting-3">
          <div class="nesting-4">
            <div class="nesting-5">
              
              <!-- Scrollable container -->
              <div class="nesting-6 overflow">
                <br> <br> <br> <br> <br> <br> <br> <br> <br>
                scroll here
                <br> <br> <br> <br> <br> <br> <br> <br> <br>
                <br> <br> <br> <br> <br> <br> <br> <br> <br>
                end
              </div>
              
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Non-flexible container -->
  <div class="static">FOOTER</div>
  
</div>

Are there any alternative solutions that are more efficient and easier than applying min-height and display:flex individually on each div in the "flexible" hierarchy up to the top element?

Answer №1

To achieve vertical alignment of elements, we can remove the display: flex property since there are no horizontally aligned elements in this layout. Instead, let's utilize the default display: block of the div element:

*, html, body {
  margin: 0;
  box-sizing: border-box;
  height: 100%;
}

.container {
  width: 300px;
  height: 500px;
  border: 1px solid black;
}

.static {
  padding: 1rem;
  height: 10%;
  background: red;
}

.middle {
  height: 80%;
}

.overflow {
  overflow: auto;
}
  <div class="container">
    <div class="static">HEADER</div>
    <div class="middle">
      <div class="nesting-1">
        <div class="nesting-2">
          <div class="nesting-3">
            <div class="nesting-4">
              <div class="nesting-5">
                <div class="nesting-6 overflow">
                  <br> <br> <br> <br> <br> <br> <br> <br> <br>
                  scroll here
                  <br> <br> <br> <br> <br> <br> <br> <br> <br>
                  <br> <br> <br> <br> <br> <br> <br> <br> <br>
                  end
                </div>

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="static">FOOTER</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

Ways to align an inner circle in the middle using CSS

div.container{ border:2px dashed blue; position:relative; } span.parent{ display:inline-block; width:40px; height:40px; border:2px solid black; border-radius:50%; text-align: center; position:absolute; right:20px; top:10px; } span.ch ...

What is causing my page to automatically refresh whenever I click on buttons?

The code snippet below shows the structure of my webpage : HTML : <button class="add-col"><i class="fas fa-columns"> Add a column</i></button> <div class="table-responsive"> <table class="table"> ...

What is the proper way to transfer data to PHP using AJAX?

I'm facing an issue while trying to send FormData to a PHP file. When I press submit, nothing happens. However, when I send data separately, it works fine. <form method="post" class="code_form" enctype="multipart/form-data& ...

Trying to access the $dirty property of a component that does not contain a <form> tag

In my AngularJS 1.6 app, I have a component that dynamically generates checkboxes using ng-repeat. Here is an example: <div ng-repeat="subItem in item.ChildItemTypes"> <md-checkbox class="md-primary" ng-model="subItem.checked">{{ subItem.D ...

Keeping an eye on the location and retrieving articles once more - Vuejs

ClassController <?php namespace App\Http\Controllers\Classes; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\ClassCategory; use App\Models\Article; use App& ...

The 'ReactType' type is not generic within material-ui@next

After installing material ui@next, I encountered an error in the node modules. ERROR in [at-loader] ./node_modules/material-ui/Avatar/Avatar.d.ts:8:15 11:31:52 web.1 | TS2315: Type 'ReactType' is not generic. 11:31:52 web.1 | ERROR in ...

Tips on retrieving an item using jQuery's select2 plugin

How can I retrieve the value (flight_no) from the processResults function in order to populate the airline name with the respective flight number when selected? I've attempted to access the (flight_no) within the processResults function, but I'm ...

The remaining visible portion of a viewport is equivalent to the height of an element

Is there a way to dynamically set a div's height so that it expands from its starting position to the end of its parent div, which is 100% of the viewport minus 20 pixels? Here is an example of how you can achieve this using jQuery: $(document).read ...

Ways to obtain the scrollWidth and offSetWidth of a div that is being utilized as an ngx-datatable-cell-template within an ngx-datatable

How can I retrieve the scrollWidth and offSetWidth of a div that is included in a child component being loaded as an ngx-datatable-cell-template inside ngx-datatable? I am consistently receiving values of 0. I have added a template variable for the div el ...

Navigating through CSS and working with partially cropped corners

I created a png file of a rounded rectangle in Photoshop, but noticed that parts of the image are getting cropped at the top right, bottom right, and bottom left corners, resulting in a square shape overall. Only the top left corner retains its rounded edg ...

Using ngTouch for ng-click events on the body element can cause issues with links and input fields on mobile

Seeking assistance with a unique issue I am facing on my app-like website. I have implemented a swipable menu but I want it to close whenever the user taps outside the menu. I have tried using ngTouch for swiping and attached ng-click="menuToggled = false" ...

The testPattern provided is not valid for configuration with Jest using react-scripts in a Mono Repo environment

I have been attempting to customize the jest.config for react-scripts using this configuration: "test": "react-scripts test -- --config=jest.config.js", Just wanted to point out that I am working with a mono repo. However, this setup ...

Adjusting the positioning of two elements inside a container

Contained within the #search-bar div is a clickable image and an h1 element (unfortunately, I am unsure where to place the relevant image.. my apologies). My goal was to separate them as shown. I'm curious if there might be a simpler and more concise ...

Angular 2 failing to display background images

I have been working on an Angular 2 app with just one component. I recently tried to set a background image for the entire page using the following code: app.component.html <dashboard class="dash"></dashboard> app.component.css .dash { b ...

Simple steps to emphasize a table cell in Angular 2

Essentially, I have a table structured as shown below <table> <tr> <td>Date</td> <td>Time</td> <tr> <table> The goal is to make both the date and time clickable within this table. When clicking on the date, ...

Extension for Chrome that switches between active and inactive modes

I have been attempting to create an extension that can toggle the functionality of another specific extension on and off. Despite numerous attempts, I have not been able to find a solution that works effectively. Essentially, my extension displays a popup ...

Strategies for updating JSON file content as data evolves

I am facing an issue with loading a json file that populates charts. The file is generated from external data sources and stored in the asset directory. Is there a method to automatically load the updated json file? I have attempted to include the code fo ...

What techniques can I use to modify an object while it's being iterated through?

I've been attempting to manipulate the object while looping through it, but unfortunately, it's not working. How can I modify it so that the constant patient includes the property lastActivity inside the this.model array? My code looks like this ...

Using react-input-mask together with a child component in React is not compatible

I've been exploring ways to mask a Material UI TextField, and after researching some solutions online, I came across this code snippet: <InputMask mask="(0)999 999 99 99" value={phone} disabled={false} ...

Tips for interrupting API requests that exceed a 10 ms response time within an interceptor

After experimenting with the implementation of takeUntil from rxjs, I noticed that clicking the Call API button multiple times does not cancel all API calls after a certain time. For a demonstration, you can check out this Stackblitz link - Code Example ...