Is there a way to not limit the height of parent elements when using the Bootstrap 4.6 grid system or Flexbox

I'm struggling to make Bootstrap/Flexbox work smoothly within a height restriction.

In my design, I have a <main> element with a set height. In the application itself, this height is calculated so that the footer aligns perfectly at the bottom of the screen. For demonstration purposes, it's fixed at 500px.

I aim to place user data of varying lengths (ranging from 1 line to potentially up to 1000 lines based on API response) in a card. This card should be limited by the height of its container, with an internal scrollbar to navigate through its content. However, the card-body seems determined to expand indefinitely to accommodate all contents - I can't seem to constrain any part of the structure by the 500px height defined on the <main> element.

Here's a simplified illustration. The dark bar represents the footer, correctly positioned below the 500px <main> element. The card extends far beyond it - what I desire is for the card to stay within the top and bottom boundaries of the page while enabling scrolling inside the card to view its contents.

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">  
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <title>Manage List</title>
    </head>

    <body>
        <main style="height: 500px">
            <div class="container-fluid">
                <div class="row">
                    <h1>Dashboard</h1>
                </div>
                <div class="row">
                    <div class="col-7">
                        <div class="card">
                            <div class="card-header">Your Items</div>
                            <div class="card-body overflow-auto">
                                    <!-- User data here -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <footer class="py-4 bg-dark"></footer>
    </body>
    
    </html>

References to similar questions:
When flexbox items wrap in column mode, container does not grow its width
Prevent flex item from exceeding parent height and make scroll bar work
Why don't flex items shrink past content size?
white-space css property is creating issues with flex
Fitting child into parent
Flex item is not shrinking smaller than its content

I've experimented with adding min-height: 0 everywhere, using flex-shrink: 1, flex-grow: 0, as well as following other tips from the mentioned questions. I'm running out of ideas except perhaps manually setting the height of each element along the hierarchy using height: 100% to ensure nothing surpasses the constraints of <main>...

Answer №1

To ensure the correct layout, you should set the container as h-100 d-flex flex-column, and then add h-100 to the column and card elements. It's important to use overflow-hidden on the row to contain its height within the container.

<main style="height: 500px">
    <div class="container-fluid h-100 d-flex flex-column">
        <div class="row">
            <h1>Dashboard</h1>
        </div>
        <div class="row overflow-hidden">
            <div class="col-7 h-100">
                <div class="card h-100">
                    <div class="card-header">Your Items</div>
                    <div class="card-body overflow-auto"> dsfg<br>dsfg (content shortened for brevity) <br>dsfg<br>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
<footer class="py-4 bg-dark"></footer>

Check out the demo here

Answer №2

To achieve the desired layout, you need to work your way down the stack and apply d-flex, h-100, and flex-column classes in the parent container.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e7eaeaf1f6f1f7e4f5c5b1abb0abb6">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script><script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8be9e4e4fff8fff9eafbcbbfa5bea5b8">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<main style="height: 500px">
    <div class="container-fluid h-100 d-flex flex-column">
        <div class="row">
            <h1>Dashboard</h1>
        </div>
        <div class="row d-flex overflow-hidden">
            <div class="col-7 h-100">
                <div class="card d-flex flex-column h-100">
                    <div class="card-header">Your Items</div>
                    <div class="card-body overflow-auto">
                            dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>dsfg<br>...
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
<footer class="py-4 bg-dark"></footer>

Answer №3

To ensure proper display, it is important to define the height or max-height for the .card-body element.
In the following code snippet, I determine the height of the <main> and the .card-body based on the viewport dimensions.

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  <title>Manage List</title>
  <style>
    main {
      padding-bottom: 1em;
      /* Viewport height minus footer height  */
      height: calc(100vh - 48px);
    }
    
    main .card .card-body {
      /* Viewport height minus footer height minus height of preceding elements minus main padding-bottom */
      height: calc(100vh - 48px - 7em - 1em);
    }
  </style>
</head>

<body>
  <main style="">
    <div class="container-fluid">
      <div class="row">
        <h1>Dashboard</h1>
      </div>
      <div class="row">
        <div class="col-7">
          <div class="card">
            <div class="card-header">Your Items</div>
            <div class="card-body overflow-auto">
              Content here
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
  <footer class="py-4 bg-dark"></footer>
</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

Surround Content with Rotated Container

I'm trying to achieve a unique layout where the heading is rotated with CSS transform rotation, and I want the body text to flow around the rotated text. The issue I'm facing is that when I apply float: left to the heading, the text wraps around ...

Linking children to their parents in a mat tree structure

I'm looking to create a mat tree based on the provided diagram. https://i.sstatic.net/cTY2w.png So far, I've managed to design the icons and boxes, but I'm struggling with drawing the connecting lines. Can anyone assist me with this part? I ...

Enhance your textbox with more detailed descriptions than just displaying NaN

I am working on a form that includes select boxes. If a user selects an option from "Convert From" and another option from "Convert To" but does not enter a number in the input field, instead of displaying NaN in the result text box, I would like to show ...

The picture won't stay within the confines of the container

As I work on my fitness site, I wanted to incorporate a sponsor section. While exploring some code that matched the style of my website, I discovered this snippet below. However, being new to CSS, I struggled with fixing it to ensure that the images fit ...

Displaying MySQL data on an HTML page with Node.js

Hello there, I recently started delving into Node.js for the first time. My current project involves fetching data from MySQL and displaying it on my HTML page. However, when I try to access my website at http://localhost:3000/index.html, I encounter an er ...

Concealing and revealing information with jQuery and AJAX

Need help hiding a Message and displaying an alert message after 5 seconds, but it's not working. What I want is for the Message to be hidden and show an alert message 5 seconds after clicking submit. <script> $(document).ready(function () { ...

Tips for preserving the contents of a list with HTML and Javascript

My latest project involves creating a website with a To-Do list feature. Users should be able to add and delete items from the list, which I achieved using JavaScript. Now, my next goal is to ensure that the current items on the list are saved when the pag ...

Developing a dynamic table using data retrieved from an API within a JS React Bootstrap framework

I'm new to JavaScript and struggling with a problem. I am trying to create a dynamic table displaying information about cars using data from an API provided by a friend (full JSON shown at the end of the post). While I can successfully fetch and displ ...

Convert the regex `[quote="author"]text[/quote]` into a well-formatted div block

I'm attempting to transform [quote="author"]text[/quote] into a properly formatted div block. function bbCode($p_text){ // capturing author $pattern = '/\[quote="(.+)"\]/'; preg_match($pattern, $p_text, $match) ...

Ways to conceal dropdown div once an area outside its parent menu is clicked?

I've got this neat jQuery script that's working perfectly. It's a basic dropdown menu setup where clicking on the "topbar" element toggles the visibility of the "topbarin" div. $(document).ready(function(){ $('.topbar').click( ...

Utilizing the ternary operator in Angular HTML templates

Using the ternary operator in HTML can simplify your code: <div> {{ showStringOneFlag ? 'Display String 1' : 'Display String 2' }} </div> This approach could save you from setting a string variable multiple times in JavaSc ...

Encountering a routing issue following the integration of Bootstrap

Embarking on a "Hack this site" project for fun, but ran into a routing error after incorporating the bootstrap gem and jquery. app/assets/stylesheets/Application.scss @import "bootstrap"; app/assets/javascripts/Application.js //= require jquery3 //= r ...

Tips for repairing a horizontal line at the bottom of the <TD> tag and placing text on top

I am working with an HTML table that contains a single TR tag and two TD tags. Each TD has two HR tags creating horizontal lines, along with some text. I am looking for a way to keep the text aligned at the top of the TD and the horizontal line at the bott ...

Object shifting as the window is resized

Check out the Umbrella image in the images section: Full-size window: . Reduced (ideal placement): I want my umbrella image to always display in the center of the screen, regardless of the size of the window. It should work when the window shrinks and ...

Using compound class selectors in RobotFramework with Selenium to target specific elements

Attempting to monitor the on-screen status of an element that transitions from : class="slide-out-div contrastBoxDark closed" to class="slide-out-div contrastBoxDark" however, when using Page Should Contain Element class:slide-out-div.contrastBoxDa ...

Display or conceal various content within div elements using multiple buttons

I have a set of 5 image buttons, each meant to trigger different content within a div. When the page loads, there should be default content displayed in the div that gets replaced by the content of the button clicked. The previously displayed content shoul ...

Navigating with style: Mastering the art of Bootstrap 4.1's vertical navs with the mr

Why isn't 'navs' the same as 'navbar'? Learn more about navs here <ul class="nav flex-column"> <li class="nav-item"> <a class="nav-link" href="#"> <i class="fas f ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

It is recommended that the page does not scroll while utilizing both the sidenav and toolbar simultaneously in Angular Material

Using md-sidenav without md-toolbar works fine. The sidenav opens correctly, as shown in the image below. https://i.sstatic.net/PjsPp.png However, when a toolbar is added before the sidenav or its containing section, opening the sidenav causes the page t ...

Having trouble getting a background image with tint to display properly in Bootstrap

I applied a sleek black transparent gradient to my body background picture, but as soon as I integrated bootstrap into the HTML code, the effect ceased to function. body { background: linear-gradient( rgba(0, 0, 0, 0.5), rgba ...