Implementing grid items in CSS Grid that do not stretch the grid row and have a container that is scrollable

Here is my dilemma:

I am looking to create a grid with a fixed number of columns (that can be adjusted via javascript) and a variable number of rows, all with equal heights.

The rows will be determined by the number of grid items, which are represented as UI cards. The goal is for these cards to fill the entire height of their respective cell without altering the overall row height. Essentially, the maximum height of each card should match the height assigned to the row by the grid.

Each card consists of three components: Header, Body, and Footer. The body component must be scrollable in case it contains more list items than the row height allows.

I have attempted to implement this concept on stackblitz

https://stackblitz.com/edit/angular-3gkmtm

However, I am facing some challenges:

  1. The cards stretch the row when additional items are added
  2. I am trying to achieve a scrollable body section within the card without manually setting a fixed height
  3. When there are more than 3 rows, an overflow issue occurs

I would greatly appreciate any assistance!

<article>
  <section>
    <h2>Fixed Grid with Scrollable Cards</h2>
  </section>
  [...]

In summary, I want to maintain a responsive grid layout where each card dynamically adjusts its height based on the content, while also enabling scroll functionality if needed.

Thank you for your help!


[...]

global style

html , body{
  height: 100vh;
  overflow: hidden;
}

Answer №1

It seems like you want the card body to fill the available space between the header and footer without expanding the card if it contains items. One way to achieve this is by adding another div with absolute positioning inside the card body, sized to fit the full body height so that overflow works correctly.

Here's a modified template for your reference:

<article>
  <section>
    <h2>Fixed Grid with scrollable cards</h2>
  </section>
  <section>
    <button (click)="onAdd()">Add</button>
    <button (click)="onRemove()">Remove</button>
  </section>
    <section class="remaining-height">
        <div class="grid-container">
      <div class="grid-item" *ngFor="let card of cards">
      <div class="card">
        <div class="card-header">Card #{{card}}</div>
        <div class="card-body">
          <div class="card-flexible-scroll">
            <div class="list-item" *ngFor="let item of list">{{item}}</div>
          </div>
        </div>
        <div class="card-footer">
            Some Footer
        </div>
      </div>
      </div>
        </div>
    </section>
</article>

And here is the updated CSS code:

.card-body{
  .list-item{
    padding: 6px;
    background:#fcd3d3;
  }
   .list-item:nth-child(even){
    background:#efefef;
  }
  position: relative;
  flex:1;
}
.card-flexible-scroll{
  position: absolute;
  width: 100%;
  height: 100%;
  max-height: 100%;
  overflow-y: auto;
}

I have set up a modified version on StackBlitz for you to see how it functions. Please let me know if this meets your requirements or if you need further clarification.

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

Automatically adjust the top margin of the content section to accommodate a fixed header height

After extensive searching, I've come across various solutions but none of them seem to fit my needs. I am a beginner/amateur developer. The issue I am facing is with a fixed header that resizes when scrolling. I understand that by adding padding-top: ...

Ways to display the chosen value based on the item's index using Javascript in Angular

If you want to view the complete code, just click on this link. I have identified the main issue here. Check out the code here: https://stackblitz.com/edit/test-trainin-2?file=src/app/app.component.html The problem is when you interact with the green bal ...

Listening for a long press in Angular 2: A step-by-step guide

Check out this link for an AngularJS example. Curious how this functionality translates to Angular 2? ...

Having trouble using jQuery's .off() method to remove an event handler?

I'm facing an issue with my jQuery code where the .off('click') method doesn't seem to be working. I've tried removing the event binding from '.reveal-menu', but it's not working as expected. The initial animation wo ...

Display corresponding div elements upon clicking an HTML select option

Is there a way to make a div visible when a corresponding select option is clicked, while hiding others? My JavaScript skills are lacking in this area. CSS #aaa, #bbb, #ccc { display:none; } The HTML (I'm using the same id name for option and d ...

`How can you indicate a clicked linkbutton from a group of linkbuttons in asp.net?`

There is a single aspx page with a set of link buttons on it. Link Button 1 Link Button 2 Link Button 3 Link Button 4 Link Button 5 When any of the link buttons are clicked, they should be highlighted. All these link buttons are contained within a table ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...

Footer is stubbornly stuck in the middle of the page, refusing to budge despite the

visit the live site here Despite setting the body height:100%, my footer continues to get stuck mid-page on certain pages such as about and contact. I have tried specifying the footer height and using position:fixed, position:absolute, and bottom:0, but n ...

Exploring the potential of utilizing Pipes in Angular with dynamically loaded components

I am facing a challenge where I need to dynamically load multiple components. To achieve this, I have set up a parent component to handle the task based on the value of this.rink, like so: ngAfterViewInit() { switch (this.rink) { case 'ITC ...

Error: The archived-solution-list.component.html file failed to load due to an unhandled Promise rejection

I am encountering the following error on the console while executing my code. Just so you know, everything was working fine before a minor modification. I would appreciate your assistance in resolving this: Unhandled Promise rejection: Failed to load ar ...

The interface 'HTMLIonIconElement' is not able to extend both 'IonIcon' and 'HTMLStencilElement' types at the same time

After upgrading my Angular Ionic app to use Angular v13 from Angular 12 with the command ng update, I encountered errors preventing me from running the application successfully. [ng] Error: node_modules/ionicons/dist/types/components.d.ts:66:15 - error TS2 ...

Retrieve main page elements from an additional page called PageSlide

I have implemented the jquery PageSlide plugin as a menu feature on my website. The plugin opens a separate page on the side of the main page to function as a menu. However, I am facing a challenge in accessing the main page's elements from this side ...

A slender gap of white space delicately separates the transformed parent from its offspring

When trying to align a parent div with a child div that has the same background-color as the parent's border-color, I am encountering an issue. Despite my efforts, there seems to be a thin line of whitespace separating the two divs. It is worth notin ...

The concept of the "Centering Effect" refers

Check out the code I have below: http://jsfiddle.net/G8Ste/577/ I want the effect to expand in all directions, not just to the right and bottom. I've experimented with margin: 0, auto, and other CSS and HTML styles to center it, but nothing seems t ...

Connecting ADFS to your Angular 2 project

I need to integrate ADFS with an Angular 2 Project. Despite it being an older technology, we specifically require ADFS for this project. After researching, I came across a similar question on Stack Overflow discussing On-Prem ADFS and suggesting the use o ...

Using electron cookies in Angular involves integrating Electron's native cookie functionality into an

I am currently dealing with electron and looking to implement cookies conditionally in my project. If the application is built using electron, I want to utilize Electron Cookies; otherwise, I plan to use Angular Cookies. However, I'm encountering diff ...

Leverage the power of Angular 2 components within your Asp.net MVC

I am currently working on an Asp Mvc project which utilizes Angular 1.4 for the frontend. While my project is not designed to be a single page application, I am interested in incorporating Angular 2 without transitioning the entire project into a SPA. Aft ...

Arranging HTML elements with jQuery - the existing script flips back and forth

I have created a code snippet on CodePen that showcases a feature of the website I am currently developing: http://codepen.io/barrychapman/pen/BzJGbg?editors=1010 Upon loading the page, you will notice 6 boxes. The first box is active, while the remaining ...

Styling CSS for HTML links

I am encountering an issue with a CSS hover effect on an image that is also a hyperlink to another website. The CSS styling for the hover effect seems to be interfering with the functionality of the HTML hyperlink. The image in question is aligned with ot ...

Initiate a CSS animation only when a different animation has completed

I am trying to create a sequence of animations using 2 squares. I want the animation for the red square to start only after the blue square's animation is complete. How can I achieve this cycle of animations? .box { width: 100px; height: 1 ...