``
Ever built a grid layout only to see it break when content overflows? If you’ve been dealing with a stubborn grid item that expands beyond its assigned space, pushing everything else out of place, then you’re in the right spot. Let’s walk through the issue and fix it the right way.
The Problem
Imagine you’ve got a CSS Grid layout with a specific item that spans multiple rows—like a transaction history panel. Inside that grid item, you’ve got a list of transactions that keeps growing.
The problem?
- As the list grows, the grid item expands beyond its allocated space, messing up your entire layout.
- Instead of keeping the grid item fixed and making the list scroll inside it, the whole thing stretches like an unruly rubber band.
What We Want
- The grid item (let’s call it
.transaction-card
) should stay within its assigned grid area. - The overflowing list inside it should scroll instead of pushing everything else down.
Why Does This Happen?
CSS Grid is great, but by default, grid items expand if their content overflows. Even though you’ve defined grid-row: 1 / span 3;
, the grid item will still grow if there’s too much content inside it.
The root cause?
- The list inside the grid item has no height constraints, so it pushes the entire
.transaction-card
beyond its grid area.
The Fix
To solve this, we need to:
-
Keep the
.transaction-card
inside its grid area by forcing it to always respect its height. -
Make the overflowing content scrollable inside using
overflow-y: auto;
.
The Correct Way to Do It
HTML
`html
Transaction Details
Transaction 1
Transaction 2
Transaction 3
Transaction 4
Transaction 5
Transaction 6
Transaction 7
+ CREATE
`
CSS
`css
/* Define a grid with fixed row sizes /
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, 1fr); / Keeps rows fixed /
height: 100vh; / Makes sure the entire grid has a fixed height */
gap: 10px;
}
/* Keep the grid item inside its assigned space /
.transaction-card {
grid-column: 3;
grid-row: 1 / span 3;
display: flex;
flex-direction: column;
height: 100%; / Ensures it fills its assigned grid space /
overflow: hidden; / Prevents it from growing beyond its limits */
}
/* Make the list scrollable inside the card /
.transactions-detail-wrapper {
flex: 1; / Fills available space inside .transaction-card /
min-height: 0; / Critical to prevent flexbox from forcing growth /
overflow-y: auto; / Enables scrolling when items exceed height */
padding: 10px;
}
/* Button stays at the bottom, doesn't get pushed up /
.create-btn {
padding: 12px 20px;
background-color: black;
color: white;
border: none;
border-radius: 5px;
flex-shrink: 0; / Ensures the button doesn’t shrink */
margin: 10px;
}
`
Why This Works
The grid item stays put
-
.transaction-card
is forced to stay inside its assigned grid area by settingheight: 100%
andoverflow: hidden;
.
The list scrolls instead of expanding
-
.transactions-detail-wrapper
takes the available space inside.transaction-card
without forcing it to grow. -
min-height: 0;
(a common gotcha) ensures that flexbox doesn’t let the list overflow its parent. -
overflow-y: auto;
makes the list scroll when there’s too much content.
The button stays in place
-
flex-shrink: 0;
makes sure the button at the bottom doesn’t shrink when the list overflows.
Final Thoughts
This solution keeps the grid layout clean and structured while ensuring that growing content is handled properly. If you’re ever struggling with an expanding grid item, check for these:
✔ Does the parent have a fixed height? (If not, the child will expand it.)
✔ Did you add min-height: 0;
inside a flexbox? (Without it, children might force growth.)
✔ Are you using overflow-y: auto;
correctly? (Otherwise, content won’t scroll.)
Now your grid layout stays intact, and any overflowing content scrolls inside its section instead of breaking everything!