Bootstrap grid columns cannot properly handle overflowing content

Utilizing bootstrap version 5.2.3

I'm facing a challenge not with bootstrap itself, but rather my limited comprehension of the flex system and overflow.

Displayed below is a scenario where the first div has a fixed height of 100vh. My goal is to keep the navigation bar, main content, and footer visible at all times.

The container containing images needs to be scrollable, hence the use of the overflow: scroll style. However, it appears to break out of its parent element, causing disruptions on the page layout. I suspect this issue may stem from the grid structure (utilizing the row and col classes).

<div class="vh-100 d-flex flex-column">
  <div>
    <h1>Navigation bar</h1>
  </div>
  <div class="flex-fill d-flex flex-column">
    <h1>Main content</h1>
    <div class="flex-fill d-flex flex-column">
      <div class="row flex-fill">
        <div class="col-6">
          <p>&Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto consequuntur harum hic inventore laborum maiores necessitatibus nihil nobis officia optio perspiciatis quaerat quas quo reprehenderit rerum sequi, veritatis, vitae?</p>
        </div>
        <div class="col-6">
          <div class="d-flex flex-column flex-wrap gap-2 overflow-scroll">
            <div class="border" style="background-color: green; padding: 5px; margin: 5px;"><h5>Description</h5><img src="http://localhost:4545/blabla.png"></div>
...
          </div>
        </div>
      </div>
      <p>&Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum itaque nemo placeat sed voluptatum! Adipisci beatae debitis deleniti dignissimos doloremque, doloribus eius facere illo in, nisi nostrum quia repudiandae sit!</p>
    </div>
  </div>
  <div>
    <h1>Footer</h1>
  </div>
</div>

Answer №1

If you're finding it challenging to implement an overflow-scroll feature in a Flex layout, you're not alone. However, with a good understanding of how Flex behaves, achieving this effect is definitely possible. Let's walk through the steps to help you achieve your desired outcome.

Let's get started.

Breaking Down the CSS

In our HTML structure, we have three main sections: #header, #main, and #footer.

To achieve the desired behavior, it's recommended to first establish fixed heights for elements like #header and #footer, and then calculate the remaining viewport height to be applied to #main.

We can simplify this process by using CSS variables. We define variables for the heights of header and footer within the :root selector to make them globally accessible throughout the stylesheet.

:root {
  --headerHeight: 100px;
  --footerHeight: 100px;
} 

We then compute the total height needed, which we label as totalHeight. By utilizing the calc() function, we sum up the values of headerHeight and footerHeight variables using the var() function.

Since both heights are set at 100px, the totalHeight will equate to 200px.

--totalHeight: calc( var(--headerHeight) + var(--footerHeight) ) //200px

This approach makes it easier to adjust the headerHeight without manually updating the totalHeight value, providing flexibility in design modifications.

Next, we assign these calculated variables to the #header and #footer elements respectively.

Now, for setting the height of the remaining content area, #main, simply subtract the totalHeight from the full viewport height (100vh).

#main {
  height: calc(100vh - var(--totalHeight));
}

Understanding the HTML Structure Using Bootstrap

The pivotal part of the layout can be summarized as:

<div class="container d-flex flex-column" id="main">
    <div class="row flex-grow-1 overflow-auto">

We begin by transforming the container into a flex environment with the .d-flex class. To vertically stack the content, we employ the .flex-column class, automatically establishing a column-based flow.

The first row inside functions as a flex item due to its direct parentage under the flex container. Applying the .flex-grow-1 class ensures this row expands to fill available space while preserving the required space for other rows.

This setup guarantees responsive behavior out of the box!

To introduce a scrollbar only when necessary, we incorporate the .overflow-auto class on this specific row.

Check out the code snippet below. For optimal viewing experience, utilize the option to display it in full page mode, open developer tools, and experiment with the responsive settings to observe how the scroll functionality kicks in as the viewport size diminishes!

Additionally, access the provided JSFiddle link for more insights.

Other Approaches

If the heights of your header and/or footer are dynamic rather than fixed, consider these alternatives:

  1. Apply a max-height to the overflowing element;
  2. Utilize JavaScript to dynamically determine the combined height of header and footer, redistributing the remaining viewport height to #main.

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 mouseover and mouseleave functions remain active even during window resizing

I wish to create a dynamic menu that appears when hovering over a user, but only if the window size is above 977 pixels. Take a look at my code below: $(document).ready(function() { $(window).on("load resize", function(event){ var windowS ...

Checking links with AngularJS and HTML 5

Good day everyone: It seems like a simple question, but despite spending hours searching online, I am still unable to find the solution: how can an URL be validated with the inclusion of www without using http. Here's what I have tried so far: I a ...

jquery - adjust floating section height

I designed a page with two distinct sides. On the right side, I have implemented a floating section using Bootstrap panels that can be dragged to different sections on that side. However, I am facing an issue where the height of the right side section doe ...

Load link dynamically using the rel attribute

I am trying to implement dynamic content loading using jQuery's .load() function. The links are stored in the .rel attribute of the anchor tags. My setup looks like this: <script> $(document).ready(function(){ $('.sidebar_link').clic ...

Element on webpage "Expands" When Scrolled into Visibility

After posting a question on Stack Overflow, I noticed discrepancies between the element dimensions reported by Chrome Inspector and Selenium WebDriver. While Chrome Inspector showed w = 979, h = 1961, Selenium returned dimensions of 979 and 1461 respective ...

Using html as a template parameter in django

When passing HTML to my template, I am using the following code: passDict["problemText"] = <p> Some html</p> return render(response,'main/base.html',passDict). However, when trying to display {{problemText}} in my HTML file, I only se ...

What is the best way to implement a sub-menu using jQuery?

After successfully implementing a hover effect on my menu item using CSS, I am now struggling to make the sub-menu appear below the menu item upon hovering. Despite my efforts to search for jQuery solutions online, I have not been successful. Are there a ...

Approaching fashion from a new angle: incorporating perspective into

How can I style elements in the perspective to appear straight(3d) instead of flat due to perspective? I want to achieve a design where the alphabets and flag post image align perfectly. https://i.sstatic.net/HcHMC.jpg .container { perspective: 100p ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

Altering the appearance of a component that is currently selected and in use

Currently, I have incorporated a component with its selector within another component as shown below: <div class="col-xl-4" style="margin-bottom: 30px;"> <app-patient-info-accordion *ngIf="patient" [cardTitle]=&qu ...

Design a CreateJS/EaselJS website, comprised of multiple web pages, that is not focused on gaming

I have developed an existing HTML5 Canvas webpage composed of multiple pages, buttons, and hotspots using pure canvas javascript code. The reason I refer to 'buttons' and 'hotspots' in quotes is because I created them from scratch in j ...

Strong tags are not functioning properly within paragraph tags in Firefox

It has come to my attention that when I insert <strong> into a <p>, the text does not appear bold. This issue seems to only occur in Firefox, as Chrome and Safari display it correctly. However, when I place <strong> inside a <li>, e ...

List the acceptable favicon formats for Internet Explorer version 10 and above

When I say type, I'm referring to the file extension. My favicon displays correctly in Firefox, Chrome, and Safari, but someone suggested that the issue may be related to the favicon type. Is there a reliable online resource where I can find informa ...

The checkbox appears unchecked, yet the content normally associated with a checked checkbox is still visible

As the title suggests, I am encountering an issue with my code. Here is the snippet: $('#somediv').change(function() { if(this.checked) { $('.leaflet-marker-pane').show(1); } else { $('.leaflet-marker-p ...

Using a For Loop in VueJS with TypeScript for an array of objects

I'm currently working on a for loop within an object array. Below is the code snippet I am working with: private async rightsOverview() { let item: any[] = []; const prod = await fetchFromApi<ProductionBaseType>(`/productions/${id ...

The user can witness the appearance of a scrollbar when utilizing the

Struggling to remove the scroll bar from my ion-scroll. Have tried various methods but nothing seems to work. Need assistance with this, please! Attempted using all attributes and even used CSS like ::-webkit-scrollbar with display: none;, which hides it ...

Ways to display and conceal menu depending on initial view and scrolling

On my website, I have implemented two menus - one to be displayed when the page loads and another to show up when a scroll occurs. You can view my page here. The goal is to have the white part visible when the position is at the top, and switch to the blu ...

The alignment of text in the flex column is incorrect on the right side

The flex column I created is having an issue with the arrow on the right side. It's not displaying properly and is hidden. The arrow should be similar to the one on the left and point to the left direction. I have used left: 0; for the left side and ...

I'm having difficulty grasping how this CSS is being used

There's something strange about the way this CSS is being used, and I can't quite wrap my head around it. .tablecontainer table { ... } I'm familiar with table.tablecontainer, which indicates that the class attribute is equal to "table ...

Enhancing jQuery UI Tabs with HoverIntent

I'm currently working on incorporating HoverIntent into my jQuery tabs, but I seem to be facing some issues. Below is the code snippet that I've been using. Any suggestions or tips would be greatly appreciated. Thank you! The code was sourced di ...