Ensure that each row in the grid expands to occupy all remaining space, with the option to scroll if additional space is required

Looking to have a grid item expand to fill 100% of available space, but also scroll if it exceeds that space.

Here's an example scenario where I'm having trouble with the CSS line marked: '/* DOESN'T WORK */' when attempting to set overflow-y to scroll.

Specific requirements:

  • The cart should take up 100% of its container.
  • The checkout button should always be positioned at the bottom of the cart, regardless of the number of items.
  • Cart items should fill up the available space, then overflow and scroll once the space is filled.

const item = `
  <div class="cart__item">
    T-Shirt
  </div>
`;

const cartItems = document.getElementById("cart__items");

const renderItems = (n) => {
  if (n < 1) {
    cartItems.innerHTML = "cart emtpy";
    return;
  } 
  cartItems.innerHTML = item.repeat(n);
}

let numItems = 1;

const incItems = document.getElementById("inc_items");

incItems.addEventListener("click", () => {
  numItems++;
  renderItems(numItems);
});

const decItems = document.getElementById("dec_items");

decItems.addEventListener("click", () => {
  if (numItems > 0) { numItems--; }
  renderItems(numItems);
});

renderItems(numItems);
.container {
  height: 150px;
  width: 500px;
  background: pink;
}

.cart {
  display: grid;
  height: 100%;
  grid-template-rows: 1fr max-content;
}

.cart__item {
  outline: 2px solid red;
  color: white;
  background: blue;
}

.cart__items {
  display: flex;
  place-items: center;
  flex-direction: column;
/*   overflow-y: scroll; */ /*  DOESN'T WORK */
  outline: 2px solid black;
}
<button id="inc_items">Inc Item</button>
<button id="dec_items">Dec Item</button>
<div class="container">
  <div class="cart">
    <div id="cart__items" class="cart__items"></div>
    <div class="cart__checkout">
      <button>Checkout</button>
    </div>
  </div>
</div>

Answer №1

To successfully size the elements within the .cart class, you can set the width and height to 100%:

width: 100%;
height: 100%;

Additionally, remember to uncomment the line in .cart__items:

overflow-y: scroll;

Alternatively, you can let the browser manage the overflow with:

overflow: auto;

Feel free to review the snippet below:

.container {
  height: 150px;
  width: 500px;
  background: pink;
}

.cart {
  display: grid;
  grid-template-rows: 1fr max-content;
  width: 100%;
  height: 100%;
}

.cart__item {
  outline: 2px solid red;
  color: white;
  background: blue;
}

.cart__items {
  display: flex;
  place-items: center;
  flex-direction: column;
  overflow: auto;
  /* or overflow-y: scroll; */
  outline: 2px solid black;
}
<button id="inc_items">Inc Item</button>
<button id="dec_items">Dec Item</button>
<div class="container">
  <div class="cart"> 
    <div id="cart__items" class="cart__items">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dignissim felis non felis mollis, ac pellentesque libero tincidunt. Quisque commodo felis vitae mi facilisis placerat eu a dui. Vestibulum ultrices accumsan elit a sollicitudin. Mauris porta, leo sit amet condimentum tincidunt, velit nunc ultricies nibh, imperdiet elementum felis sem sit amet ex. Proin molestie urna sed vulputate ornare. Sed vel pulvinar sem, sit amet condimentum ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dignissim felis non felis mollis, ac pellentesque libero tincidunt. Quisque commodo felis vitae mi facilisis placerat eu a dui. Vestibulum ultrices accumsan elit a sollicitudin. Mauris porta, leo sit amet condimentum tincidunt, velit nunc ultricies nibh, imperdiet elementum felis sem sit amet ex. Proin molestie urna sed vulputate ornare. Sed vel pulvinar sem, sit amet condimentum ante. </div>
    <div class="cart__checkout">
      <button>Checkout</button>
    </div>
  </div>
</div>

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

Extracting information from a JSON file is a straightforward process that involves accessing

After spending months trying to extract data from a JSON file, here is what I have encountered: [["wrere","bogdan12",31,21,"profile_pic/poza3.jpg",78,21,31,"profile_pic/download.jpg"] , ["hey men","bogdan12",31,21,"profile_pic/poza3.jpg",76,21,31,"pro ...

JavaScript button mouse enter

There seems to be an issue with this code. The button is not functioning properly after hovering over it, even though it was copied from the instructional website where it works fine. <html> <head> <title>Button Magic</t ...

The margin 0 auto feature is ineffective, and instead, the target is activated upon the page's initial loading

As a beginner in CSS, I am encountering some issues with the HTML page of my application. I am currently working on an app and aiming to create a login view where the login form appears either after a voice command or button click. I am facing two main pro ...

The left and right arrows maintain their visibility even when they are outside of the image

I am a beginner in the world of web development and I am trying to implement left and right navigation for images using arrows. My goal is to have the arrows fade in when the mouse hovers over the image and fade out when it's moved away. However, I am ...

Experiencing a hiccup in your jQuery animation?

Click here to access the fiddle demonstrating the issue. A situation arises where a span with display: inline-block houses another span that is being slowly hidden. The container span unexpectedly shifts back to its original position once the hiding proces ...

Adjust the size of the text only when there is limited space available

Is there a way to make text shrink to fit inside a container only if it's too long, but remain the same size otherwise? I'm working on a chart with keyboard commands overlaid on an image of a keyboard and want the text to adjust dynamically. Any ...

Tips for organizing unordered list items into columns, with the text aligned at the top

I typically utilize the following method to arrange my list items into columns: ul.col-list > li {width: 30%; display: inline-block;} However, I have encountered an issue where if the text within a list item overflows onto the next line, it pushes dow ...

Tips for dynamically loading a modal

Hello, I am curious about dynamically loading modals on different images within my current webpage. https://i.sstatic.net/yhTgs.png For example, when the life of Pi image is clicked, a modal pops up displaying relevant information. https://i.sstatic.net ...

Filtering data within a specific date range on an HTML table using JavaScript

I am attempting to implement a date filtering feature on my HTML table. Users should be able to input two dates (From and To) and the data in the "Date Column" of the table will be filtered accordingly. The inputs on the page are: <input type="date" i ...

Leveraging font awesome ttf in conjunction with scss

I am currently attempting to incorporate Font Awesome with SCSS. @font-face { font-family: 'awesome'; src: url('../../fonts/font-awesome/fa-light-300.ttf') format('ttf'); } Although the path appears to be correct, I am ...

"The space between the header-main section and the main-footer section is pure white

I'm facing an issue with the white space between the header-main and main-footer sections. I would like to either match their color to the rest of the website or completely remove the space. The color I want to use is #fefcf5. I hope this edited versi ...

What is the reason behind the `h-full`/`height:100%` not completely filling the void within a div element?

The issue lies in the structure of my content, which includes a title, a subtitle, and a footer. My desire is for the subtitle's div to fill the gap between the title and the footer. I attempted using h-full on the div element, but it had no noticeab ...

Calling JavascriptInterface in an H5 environment is not permitted

Utilizing the power of vue, I created an immersive h5 page within the application framework. The seamless connection between the page and the app relies on the use of window.JSInterface for communication. My current challenge lies in invoking the JSInterfa ...

What is the best way to position an image using css?

I am looking to position an image on the left side of my homepage using CSS instead of HTML. However, I am having trouble figuring out how to make it work. The image I want to use is called Nobullying.jpg. HTML: <html> <head> <link re ...

Using Ajax to fetch project data from an API

Currently immersed in a personal coding project, I'm struggling to troubleshoot my code. Essentially, I need to retrieve data from an API and display it on my HTML page. The data type is "jsonp" due to CORS errors that required this workaround for de ...

Responsive grid made with HTML and CSS to create dynamic DIVs

Currently, I am working on a project that requires a responsive page layout using divs. The layout should resemble a grid, with 3 columns that reorganize into 2 columns on smaller screens. Here is the basic HTML structure I have so far: <div id="main" ...

Shift designated list item to the right with overflow handling

A current project involves working on a sidebar created in bootstrap, and I've encountered an issue. I can't seem to move a selected item to the right and have it flow over the nav div. I attempted to increase the z-index with no success. One su ...

Tips for revealing a position: absolute div that is currently hidden with display: none styling

Below is the code for a div element that I want to temporarily hide using JavaScript: <div id="mydiv" style="position: absolute; top: 60px; left:5px; right:25px; bottom:10px;"> </div> After hiding it with display:none in my ...

Does SCSS have a specific 'self' identifier?

I am looking to incorporate SCSS styles on the body by default, and then reapply them specifically to the P and LI tags (since I do not have complete control over the site and want to prevent plugins from interfering with my p's and li's). To ac ...

Once I incorporated Bootstrap into my project, I noticed a noticeable white space between the div elements

Welcome to My Code Playground I was sailing smoothly with my project until I decided to include Bootstrap. Seems like there's something missing in the details, so here I am reaching out. If you spot the issue, please feel free to correct my code. &l ...