Creating a dynamic table inside a flex-grow container without disrupting the layout: A step-by-step guide

Currently, I am in the process of designing a responsive layout using Bootstrap 5.3 which consists of a NavBar, SideBar, ContentPanel, and StatusBar. The goal is to ensure that the application always occupies 100% of the view-height and view-width, displaying all necessary information without triggering page scrollbars!

Within the ContentPanel, there is an element at the top, a table containing dynamic values, and another element at the bottom. I aim for the table to utilize as much height as possible without displacing the element below it or extending beyond the viewport, impacting the StatusBar!

Everything looks good when there are only a few rows in the table, like this:

https://i.sstatic.net/TzNrx.png

However, issues arise as the table accumulates more rows: the table starts pushing the element below it and the StatusBar out of the viewport, triggering a vertical scrollbar for the entire page. An example of this is shown here:

https://i.sstatic.net/5YzyJ.png

If the content within the table exceeds the size of the ContentPanel, I wish for a scrollbar to be visible inside the table itself, confined within the ContentPanel. This way, users can scroll through the table rows without losing sight of the StatusBar. The table should occupy the remaining height of the ContentPanel after the other elements have been rendered.

Thus far, I have only managed to achieve this by setting a maximum height on the table, but this solution compromises responsiveness, as illustrated here:

[

Would appreciate any assistance with this dilemma. Here is my example on StackBlitz: https://stackblitz.com/edit/jyee1m?file=index.html

Answer №1

Utilize the vh-100 class to apply height: 100vh to the main <div> element encapsulating the entire webpage. This class, combined with a vertical flex layout, serves to constrain the sizing of internal elements.

To counteract the default min-height: min-content imposed by vertical flex layouts, we introduce min-height: 0 to specific inner elements.

By utilizing mh-100 and setting max-height: 100%, a vertical flex layout is invoked to enforce a height restriction on the ContentPanel <div> in relation to its parent container. This ensures that the inner .scrollable-table element is constrained accordingly.

.scrollable-table {
  overflow-y: auto;
}

.min-h-0 {
  min-height: 0;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8885859e999e988b9aaadfc4d9c4d8">[email protected]</a>/dist/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <link
      href="https://getbootstrap.com/docs/5.3/assets/css/docs.css"
      rel="stylesheet"
    />
    <title>Bootstrap Example</title>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2825253e393e382b3a0a7f64796478">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body class="m-0 p-0 border-0 bd-example bd-example-flex">
    <div class="vh-100 d-flex flex-column vh-100">
      <div class="d-flex flex-grow-1 min-h-0">
        <nav class="border p-2">NavBar</nav>
        <aside class="border p-2">SideBar</aside>
        <div class="border p-2 flex-grow-1 d-flex flex-column min-h-0 mh-100">
          ContentPanel

          <div>Element above table</div>
          <div class="scrollable-table min-h-0">
            <table class="table" class="table-responsive">
              <thead>
                <tr>
                  <th scope="col">#</th>
                  <th scope="col">First Name</th>
                  <th scope="col">Last Name</th>
                  <th scope="col">Username</th>
                </tr>
              </thead>
              <tbody> <!-- Table content omitted for brevity -->
              </tbody>
            </table>
          </div>
          <div>Element below table</div>
        </div>
      </div>
      <div class="border">StatusBar</div>
    </div>
  </body>
</html>

Answer №2

To achieve the desired layout, it's essential to assign a fixed height to the status bar to maintain consistency across different screen sizes. Additionally, apply the CSS property max-height: calc(100vh - 27px); to the content wrapper and utilize the Bootstrap class overflow-auto on the scrollable table to ensure the scrollbar appears within the table's boundaries. Finally, ensure the content wrapper has the classes d-flex and flex-column.

Refer to the example below for implementation details.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cbc6c6dddadddbc8d9e9a88989895">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
  <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet" />
  <title>Example using Bootstrap</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="453730302b2c2b2d3e1f3d2f0d1a6533832381">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
  <style>
    .status {
      height: 25px;
    }
    
    .content {
      max-height: calc(100vh - 27px);
    }
  </style>
</head>

<body class="m-0 p-0 border-0 bd-example bd-example-flex">
  <div class="vh-100 d-flex flex-column">
    <div class="d-flex flex-grow-1">
      <nav class="border p-2">NavBar</nav>
      <aside class="border p-2">SideBar</aside>
      <div class="border p-2 flex-grow-1 content d-flex flex-column">
        ContentPanel

        <div class="above-table">Element above table</div>
        <div class="scrollable-table overflow-auto">
          <table class="table" class="table-responsive">
            <thead>
              <tr>
                <th scope="col">#</th>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Username</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th scope="row">1</th>
                <td>Mark</td>
                <td>Otto</td>
                <td>@mdo</td>
              </tr>
              <tr>
                <th scope="row">2</th>
                <td>Jacob</td>
                <td>Thornton</td>
                <td>@fat</td>
              </tr>
              ...
              Additional table rows go here
              ...
              <tr>
                <th scope="row">20</th>
                <td>Larry</td>
                <td>The Bird</td>
                <td>@twitter</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="below-table">Element below table</div>
      </div>
    </div>
    <div class="border status">StatusBar</div>
  </div>
</body>

</html>

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

The difference in percentage padding rendering is specific to Firefox only

In my current project, I have created various responsive boxes for displaying news articles on the website. Everything seems to be functioning well except for the news items within the slider at the top of the main content. Surprisingly, they are displayin ...

Having an issue with Local Storage returning undefined

I've been working on a form to input values and show the data on a different page after submission. Below is my form: <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" hr ...

Incapable of grasping the intricacies of condensed

I recently came across a code snippet on geekcode(dot)tk and I have a question about it. Specifically, I would like to understand why the section within the table data tag is written in that particular way (at the bottom of the provided code). Thank you fo ...

general declarations take precedence over media queries

One of my usual CSS rules looks something like this: #dayslist > div { height: 51px; } Then there's the media query CSS rule: @media (max-width: 979px){ #dayslist > div { height: 44px; } } However, whenever I resize my bro ...

Troubleshooting challenges arise with Bootstrap's button group spacing issue

I am encountering spacing issues with the markup provided below. <h2>Profitability Report</h2> <form method="post"> <div class="row"> <div class="col-md-12"> <div class="btn-group btn-group-toggle ...

The most recent release of Angular UI Bootstrap is being set up for installation

Currently, I am attempting to obtain the most recent version (0.13.0) of the angular-bootstrap repository. Usually, my process involves using 'npm install', but unfortunately, the angular-bootstrap npm package remains at version 0.12.0. To addr ...

When an absolute positioned DIV is nested inside a relatively positioned DIV, it disappears when the page is scrolled

A unique feature of my DIV is that it remains fixed at the top of the page as you scroll. This is achieved by setting its position to fixed. However, within this main container, I have another div with a relative position. While the content in the relative ...

Creating a dropdown menu in Bootstrap 5 without using any of the Bootstrap

In my Angular application, I have a header with icons and pictures that I would like to use as dropdown menus. The code snippet for this functionality is shown below: <li class="nav-item dropdown"> <a class="nav-li ...

Verify the text file for any data, and if it contains any, display it on the web browser using JavaScript

I have a program in C that works with a temperature sensor. It creates a file to store the temperature and indicates whether it falls within specific values. I want to display this data on a web browser and update it every 5 minutes. I'm looking for ...

Trouble accessing modal in custom WordPress theme incorporating Bootstrap

Hey everyone! I've been struggling all day trying to open a modal dialog box in my custom theme using Bootstrap 5.2 function load_css() { wp_register_style('bootstrap', get_template_directory_uri().'/assets/css/bootstrap.min.css&apo ...

Setting the width of an image within an iframe: A step-by-step guide

Is there a way to adjust the width of an image within an iframe? Typically, if an image with high resolution is placed inside an iframe, the iframe becomes scrollable by default. ...

Unable to change the variable for the quiz

Currently, I am in the process of developing a quiz app and I am facing an issue with my correct variable not updating. Whenever I trigger the function correctTest() by clicking on the radio button that corresponds to the correct answer, it does get execut ...

Leveraging the power of jQuery and Bootstrap within an application customizer and across various web

Deciding to utilize Bootstrap for customization is a personal choice of mine. Currently, I am considering incorporating jQuery and Bootstrap in both the application customizer and multiple web parts. I am seeking advice on the most effective approach for ...

What could be the reason for having two HTML nodes within a one-node HTML document?

My goal is to use DOMdocument to retrieve a list of HTML nodes in order to create a visually appealing CSS3 tree visualization on a web page. However, I've encountered an issue with the code snippet below: // Compressed, testing HTML $text = '&l ...

What is the best way to update the video source upon clicking a link with JQuery?

Is there a way to change the video src using jQuery when a link is clicked? For example, if the user clicks on "#staff", the video src will be updated. <ul class="nav nav-pills" > <li role="presentation" class="loginPills"><a ...

How to choose an option from a dropdown menu using React

In a React application, I am creating a straightforward autocomplete feature. The code is outlined below. index.js import React from 'react'; import { render } from 'react-dom'; import Autocomplete from './Autocomplete'; co ...

Implementing a specialized CSS handler for Node.JS

Currently, I have set up a Node.JS server for a new project at my workplace. As part of the project, I have created an optimizer function that removes unnecessary elements such as tabs, newlines, and comments from HTML, JavaScript, and CSS files. Strangel ...

How come my Calendar is not showing up on the browser?

I came across a helpful guide on setting up a Calendar in HTML using JavaScript You can find it here: Unfortunately, the code I tried to use based on that guide isn't functioning as expected. <div class="row"> <div class="col-lg-4 text ...

A guide on utilizing jQuery to increase the value of an index

When the server returns JSON in this format: "games": [{ "gameType": "RPG", "publishers": [{ "publisher": "Square", "titles": [{ "title": "Final Fantasy", "gameReleases": [ 2006, 2008, 2010, 2012, 2013, 2014 ...

Top-rated program for creating user guides using HTML code

I am currently working on a project that involves creating end-user documentation for a software application. However, the software will be retired next year, so I do not want to spend too much time on producing a professional-grade manual. The documentati ...