Ensure that the <div> element expands and takes up all available space on the screen, without exceeding the viewable screen area

Is it possible to size a div element so that it occupies all available remaining space, but if its content exceeds the available space, the overflow is contained within without resizing or changing the height of the element? The element should have a fixed height that fills the remaining space.

My example utilizes Bootstrap.

In the code snippet provided above, the red box represents the area of the screen affected by the code. The horizontal length of the red square (poorly drawn) should never be less or more than the screen height minus the combined heights of all other elements on the screen.

I want the sizes of the elements to stay consistent and the list to scroll within its parent column.

I've achieved a similar effect by setting the max-height to 520px:

<div class="row mx-0 flex-grow-1" style="max-height: 520px;">
. This gives an indication of what I'm aiming for.

However, I am looking for a responsive solution that works regardless of screen height.

Update

After applying @RokoC.Buljan's answer to my work, I obtained the following layout with the desired nested scrollable areas. Thanks again for the assistance. This example uses Bootstrap 5.1.

html {
    font-size: 14px;
    position: relative;
    height: 100%;
}

@media (min-width: 768px) {
    html {
        font-size: 16px;
    }
}

.site-root {
    display: grid;
    grid-template-rows: auto 1fr auto;
    height: 100%;
}

.footer {
    position: static!important;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f4d40405b5c5b5d4e5f6f1a011d011c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<body class="site-root h-100">

    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <h3>Header</h3>
            </div>
        </nav>
    </header>
    
    <main class="container d-flex flex-column flex-grow-1 overflow-hidden">
        <div class="row"><h1>Jumbotron</h1></div> <!--Hub Jumbotron-->
        <div class="row flex-grow-1 overflow-auto" style="flex-flow:nowrap;">
            <div class="col-3 flex-grow-1 overflow-auto">
                <!--Left Side Panel-->
                <h3>Left Panel</h3>
            </div>
            <div class="col-9 d-flex flex-column flex-grow-1">
                <div class="row"><h3>Hub Nav</h3></div> <!--Hub Nav-->
                <div class="row flex-grow-1 overflow-auto" style="flex-flow:nowrap;">
                    <div class="col-3 d-flex flex-column flex-grow-1">
                        <!--Friend List-->
                        <div class="row"><h4>Friends List</h4></div>
                        <div class="row flex-grow-1 overflow-auto" style="flex-flow:nowrap;">
                            <div class="col flex-column flex-grow-1 overflow-auto">
                                <div class="p-2">Friend</div>
                                <div class="p-2">Friend</div>
                                <div class="p-2">Friend</div>
                                ...
                            </div>
                        </div>
                    </div>
                    <div class="col-9 d-flex flex-column-reverse flex-grow-1">
                        <!--Chat-->
                        <div class="row"><h4>Message Bar</h4></div>
                        <div class="row flex-grow-1 overflow-auto" style="flex-flow:nowrap;">
                            <div class="col flex-grow-1 overflow-auto">
                                <p style="height:300vh;">1. Some paragraph to force scrollbars...</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <footer id="site-footer" class="border-top footer">
        <div class="container">
            <h3>Footer</h3>
        </div>
    </footer>
</body>

Answer №1

To achieve the desired effect, make sure to use the flex property on the items you want to align, and remember to add overflow: auto; to the elements that need independent scrolling:

* { margin: 0; box-sizing: border-box; }

body {
  height: 100vh;
  display: flex;
  flex-flow: column nowrap;
  overflow: hidden;
}

.flex {
  flex: 1;
  overflow: auto;
}

.row {
  display: flex;
  flex-flow: row nowrap;
}

.cell-3  { flex-basis: 25%; }
.cell-9  { flex-basis: 75%; }
<header class="row" style="background: #bf0">
  HEADER<br>
  WELCOME!
</header>

<main class="row flex">
  <section class="cell-3 flex" style="background: #0bf">
    <p style="height: 200vh">Some paragraph to force scrollbars...</p>End.
  </section>
  <section class="cell-9 flex" style="background: #f0b">
    <p style="height: 300vh">Some even taller paragraph as well...</p>End.
  </section>
</main>

<!-- PS avoid inline `style`. The above is just for demo -->

This approach builds upon the solution provided in this answer: Flex max height content with dynamic header and footer

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

What is the best method for validating multiple fields in a form with Javascript?

I am currently working on validating multiple fields within a form. Although I lack experience in JavaScript, I have managed to piece together a code that is functional for the most part. If all fields are left blank, error messages prompt correctly. Howe ...

Unable to locate a resolution for the error message "Warning: Task "uglify" not found"

Below is the content of my Gruntfile.js: module.exports = function(grunt) { require('load-grunt-tasks')(grunt); grunt.initConfig({ uglify: { start: { files: { 'js/script.min.js': ['js/script.js&a ...

My website is being cut off

After creating this website with a viewport setup, I noticed that it is not fully visible on certain screens. When viewed on a CRT monitor at 800x600 desktop resolution or lower than 1280x800, such as on mobile devices, the content gets clipped. Is there a ...

What is the best way to adjust the size of the box while ensuring that the text remains centered inside it

Consider the HTML snippet below: <div class='title'><h2>Title</h2></div> I attempted to adjust the size of the box with the following CSS: .title { display: block; border-radius: 12px; border: 1px solid; } The resultin ...

Is it possible to target the sibling of the currently selected or focused element with CSS?

I'm attempting to add button functionality to show and hide errors using CSS. By clicking the CSS button or link, I can target the siblings of the focused element as shown below. This method is only effective in IE8+ browsers. #ShowErrors:focus + a ...

Issue with the alignment of DIV container due to spacing issues

In the screenshot below, there is a DIV container with padding of 15px on the left and right. However, the contents are appearing spaced out downwards for some reason... Other than the left & right padding, no other styling seems to be affecting it. Adjus ...

troubleshoot clientWidth not updating when CSS changes are made

Summary: When I adjust the size of a DOM element using CSS, its directive fails to acknowledge this change even with a watch on the size. Aim I have multiple custom directives (simple-graph) each containing an svg. My goal is to enlarge the one under the ...

The :after pseudo-element in action with multi-line text

Can the styling of the :after pseudo-element be applied to every line of multi-line text? Here is the code I am currently using: .container { width: 50px; } .text { position: relative; } .text:after { content: ''; position: ...

Font Awesome icons displayed using the <i class="icon-ok"></i> markup are not styled correctly and do not respond to events in Chrome

I implemented Font-Awesome, an iconic font designed for use with Twitter Bootstrap. Here is the markup: <i class="icon-remove reset-preference-button"></i> And here is the CSS: .reset-preference-button { cursor: pointer; } For JavaScript f ...

Incorporating Google font into Bootstrap using Next.js

I want to load a Google font following the instructions outlined here: https://nextjs.org/docs/basic-features/font-optimization When I follow the documentation and load it like this, everything works fine: import { Open_Sans } from '@next/font/google ...

How can I specifically target and count children from a certain class using CSS selectors?

Consider this HTML structure with items arranged at the same level: <div class="h2">Title: Colors</div> <div class="pair">Hello world (1)</div> <div class="pair">Hello world (2)</div> <div class="pair">Hello wo ...

Modify the characteristics of a bootstrap button depending on certain conditions

I am working on enhancing a bootstrap dropdown button by implementing a functionality where it will be disabled if certain conditions are met. Below is the current code snippet for the button: <> <DropdownButton title={fixtureDetails.h ...

What are some ways to enable text highlighting when it is disabled?

I've encountered an issue with text highlighting in my asp.net web application when using the latest versions of FireFox and Google Chrome. Strangely, pressing CTRL+A also does not work for all input fields. I haven't had the opportunity to test ...

Retrieve the checkbox value and assign it to an object in the $scope of an Angular

As a newcomer to angularjs, I've been struggling with this code snippet for the past two days. My goal is to retrieve values from selected checkboxes within my angular js controller. I have experimented with both ng-true-value and ng-false-value, bu ...

Build a Container Div using DOM/XPATH techniques

I have just initialized a new DOM XPATH OBJECT. After performing several operations, I saved my result using SaveHtml $String[] = $dom->saveHTML(); Next, I placed the content into a file. file_put_contents($filename, $string); The HTML Structure l ...

Updating the HTML markup within ASP.NET on the server side

When the html file loads, I need to change a specific string within it. Here is an example of the html file: <html> <head> <title>MyTitle</title></head> <body> Some Text <script type='text/javascript'> / ...

Troubleshoot: Bootstrap Link Hover Effect Not Displaying

While learning how to style links with Bootstrap, I came across this tutorial. However, I encountered a problem when I tried adding the classes badge badge-info to the a tag. It doesn't display the hover effect when I hover over it with the mouse, but ...

Locate every div element that contains a span id matching any word in a specified list

I'm attempting to create a script that will add a class based on whether or not a list item's span id contains certain words from a list. The list follows this structure: <div id="main_list"> <li class> <a data-item_ ...

I'm attempting to make it so that when users click on this link, it opens in a new tab instead of directing them away from my website. Unfortunately, using _blank

The link I have is currently opening in the same tab, but I want it to open in a new tab to keep users on my site. I've added target="_blank" to the code, but it's not working as expected. <BottomNavigation className={classes.root}> &l ...

Pressing the button in the navigation bar results in an expansion of the navbar

Can someone help me figure out why my navbar's height is different from the example on this Bootstrap navigation bar snippet? Here's the code I used: https://www.w3schools.com/bootstrap4/tryit.asp?filename=trybs_navbar_form&stacked=h I copi ...