How to Keep Subgrid Row Sizes When Gap Differs from Parent?

I'm experimenting with CSS subgrid and ran into a problem with mismatched gap values.

Kevin has this wonderful video on subgrid (https://www.youtube.com/watch?v=Zddz_R1RnfM&t=832s&ab_channel=KevinPowell), where he shows product cards using grid-template-rows: subgrid.

In his demo, the gap on the subgrid matches the gap of the parent grid, and everything lines up nicely. But when I change the subgrid's gap, or the parent gap, the row sizing no longer behaves as expected.

Code pen link: https://codepen.io/kevinpowell/pen/RwvWoLg?editors=1100

For example, changing this:

.product {
-  gap: 1rem;
+  gap: 2rem;
}


causes the first row height (with the image) to become 112px instead of the expected 120px, which is set in the parent with:

.grid {
  grid-auto-rows: 120px;
  gap: 2rem;
}


Is there a way to keep the row sizing consistent from the parent grid even when using a different gap value in the subgrid?

Tested with Google Chrome Version 138.0.7204.157 on Ubuntu
image.png
YouTubeKevin Powell
https://kevinpowell.co/courses?utm_campaign=general&utm_source=youtube&utm_medium=containersubgrid 👈 Looking to get better at CSS? I’ve got a range of courses, including several free ones!

Container queries and subgrid are now both supported in all evergreen browsers, which is super exciting, and as a bonus, they can work really well toget...
Subgrid & Container Queries change how we can create layouts
Was this page helpful?