What is the best way to ensure that a list-group scrolls within a div instead of adjusting its height?

I have a div on my webpage that resembles a receipt. This div dynamically adds X items, with a full height of 95% for graphic design purposes. The problem arises when the div reaches 95% of the screen height and a new item is added, causing the entire page to become scrollable instead of just the list-group where the items are added.

Here is an overview of my layout:

            var products = [
        { desc: "Ham", quantity: 2, price: 2 },
        { desc: "Beef", quantity: 1, price: 15 },
        { desc: "Tomatoes", quantity: 6, price: 3 },
        { desc: "Pandoro", quantity: 5, price: 22 },
        { desc: "Yogurt", quantity: 3, price: 5 },
      ];
      
      $(".total").on("click", function () {
        $(".btn-finish").removeAttr("disabled");
        $(".list-group").append(
          $("<li>", {
            class: "list-group-item d-flex justify-content-between",
          }).append(
            $("<span>", { class: "desc" }).text(products[0].desc),
            $("<span>", { class: "quantity" }).text(products[0].quantity),
            $("<span>", { class: "price" }).text(products[0].price)
          )
        );
      });
html, body {
 height: 100%;
}

.receipt {
  position: relative;
  background: #f8f7f5;
  max-height: 95%;
  padding: 1rem;
  font-size: 1.4rem;
}

.receipt:after {
  content: " ";
  display: block;
  height: 24px;
  position: absolute;
  left: 0;
  right: 0;
}

.receipt:after {
  bottom: -14px;
  background: linear-gradient(135deg, #fcfcfb 25%, transparent 25%), linear-gradient(225deg, #fcfcfb 25%, transparent 25%), linear-gradient(315deg, #00383e 25%, transparent 25%) -7px 0, linear-gradient(45deg, #00383e 25%, transparent 25%) -7px 0;
  background-size: 24px 24px;
  background-color: #dcdcdb;
}

.list-group-item {
  border-color: #fff;
  border-radius: 0.25rem;
  margin-bottom: 1rem;
  border-top-width: 1px !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa8a5a5beb9beb8abba8afee4ffe4f9">[email protected]</a>/dist/css/bootstrap.min.css" />

<div class="row h-100">
  <div class="col-5">
    <div class="receipt">
      <h4>Your Cart</h4>
      <hr />
      <ul class="list-group"></ul>
      <div class="alert alert-dark total" role="alert">
        Total
        <span class="price float-right">€0.00</span>
      </div>
      <div class="alert alert-success" role="alert">
        Loyalty
        <span class="float-right">001230013212</span>
      </div>
    </div>
  </div>
  <div class="col-7">
    STUFF
  </div>
</div>

To add items dynamically, simply click on the button with the class .total

Any ideas on how to make only the list-group scrollable when the .receipt reaches its maximum height?

Answer №1

To ensure the list-group is scrollable, I have applied the following CSS:

.list-group{
  overflow:scroll;
  -webkit-overflow-scrolling: touch;
}

Additionally, upon page load, I dynamically set the height of the list-group to match its container's height minus the height of static elements within it. This is achieved with the following JavaScript code:

var height = $(".scontrino").height();
$(".list-group").css("max-height", height - 250);

By adjusting the height of the .list-group dynamically in this manner, it now functions as intended.

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

Is less.js capable of compiling CSS code that is compatible with all browsers?

Is there a simple way to convert my Less code into browser-compatible CSS like the example below? #grad1 { background: -webkit-linear-gradient(red, blue); /* For Safari 5.1 to 6.0 */ background: -o-linear-gradient(red, blue); /* For Opera 11.1 to ...

Scrapy Library's Response.css function fails to retrieve data from API, resulting in an empty list

scrapy shell 'https://www.samsung.com/in/smartphones/galaxy-m/' fetch('https://searchapi.samsung.com/v6/front/b2c/product/finder/gpv2?type=01010000&siteCode=in&start=1&num=12&sort=onlineavailability&onlyFilterInfoYN=N& ...

Creating a custom HTML layout: A step-by-step guide

Struggling to create a unique layout in HTML and seeking assistance. Instead of words, I'll show you my current layout with images: While this works fine for now, if div1 becomes taller, the layout will look like this: My ultimate goal is to achiev ...

Using makeStyles() in MUI for conditional rendering

Trying to dynamically change the value of backgroundColor under the property "& + $track" using props.disabled but it seems that MUI does not recognize it. Why could that be? const useStyles = makeStyles((theme) => ({ root: { overflow: "vis ...

Overlaying a div on top of an iframe

Struggling to make this work for a while now. It seems like a small issue related to CSS. The image isn't overlaying the IFrame at the top of the page as expected, it's going straight to the bottom. Here is the code snippet: .overlay{ width: ...

WebpackError: The assignment of the property 'emulateTransitionEnd' to an undefined value cannot be completed

I've been attempting to implement Bootstrap in GatsbyJs, but it just seems to continually let me down. Is there a legitimate method to import Bootstrap without encountering the following error? WebpackError: Cannot set property 'emulateTransitio ...

What are the steps to properly create an ordered list?

.book-summary ol { counter-reset: item ; } .book-summary ol li { margin-left:10px; margin-top:5px; display:block; } .book-summary ol li:before { content: counters(item, ".") " "; counter-increment: item; } <div class="book-summary"> ...

Make an angularJS PUT request using $http

After selecting a value from the dropdown selection, I am attempting to make a PUT call in AngularJS. Although I have successfully retrieved the value from the selection, I am encountering difficulty in generating the response for the PUT call. Below is t ...

Is there a way to detect the completion of the fadeout animation before calling a function?

I am trying to create a toast message using jQuery. When the "show" class is appended to the div, I want the toast message to fade in and then fade out after a few seconds. Once the fade-out animation is complete, I need to remove the "show" class. This ...

Connected selection menu

I have noticed several discussions on this topic, but many of them rely on JavaScript or focus solely on a standard drop-down list. I have developed a PHP function that generates drop-down menus based on select queries in my database. Currently, this part ...

A new version of Primefaces has been released, introducing a sleek button feature with

How can I utilize the Primefaces version 10 button with a Font Awesome icon, resizing the icon without displaying the words ui:button? A and B are tests that I can resize successfully, but they are not buttons. C and D are buttons where the icon is resize ...

You need to click the form submit image twice in order to make it work

Struggling to get a standard opt-in form to work with an image submit button. The script works fine with a regular submit button, but the image button requires two clicks initially and then one click after unless the page is refreshed. I believe there mig ...

Tips for preventing the sidebar from extending beyond the footer

I am facing an issue with my sidebar that follows as I scroll. How can I make it stop following when it reaches the footer? Here's what I have attempted so far: $(function() { var floatPosition = parseInt($("#left_menu").css('top')); ...

If additional content has been included in the `div`, be sure to scroll all the way down to view it

I have a div that needs to automatically scroll to the bottom when new content is added. This is a common feature in chat applications where the user wants to see the latest messages without having to manually scroll down each time. How can I achieve this ...

What is the best way to determine the height of a DIV element set to "auto"?

When setting a fixed height on a div using jQuery, such as $('div').height(200);, the value of $('div').height() will always be 200. This remains true even if the content within the div exceeds that height and overflow is hidden. Is th ...

Leaflet map displaying accurate position only upon browser resize

Let me start by showing you a screenshot of the issue I am facing. Check it out here The screenshot clearly shows a gap between my expandable left navbar and the left border of the map image. However, when I resize the browser window by dragging and drop ...

Unexpected behavior with animation duration in Safari

We have a project in the works that involves CSS animation. Our goal is to update the animation duration using JavaScript. Although this change is effective in most browsers, it seems to have trouble with Safari and iOS. Below is the code we are using to ...

Adjustable CSS display: an adaptable left column paired with a changeable fixed right column

I am looking to align the content of a div element in a single row while dealing with an unknown width of the ul element due to varying child li elements. The h2 will always take up the remaining space, and each li element has a width of 20px. The desired ...

The nested navigation link disappears suddenly on Internet Explorer 9

This query closely resembles another issue: Nested menu keeps vanishing on IE 8 & 9. However, the solution provided is quite limited. Why do my nested menus disappear before I can reach them with my cursor unless I move it swiftly? This peculiar behavi ...

Using `href="#"` may not function as expected when it is generated by a PHP AJAX function

I am facing an issue with loading html content into a div after the page has loaded using an ajax call. The setup involves a php function fetching data from the database and echoing it as html code to be placed inside the specified div. Here is my current ...