<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy" -->

---
title: Grid ideas: Creating a CSS subgrid utility class for rows
description: A walkthrough of building a reusable CSS subgrid utility class for aligning grid item rows (headings, images, paragraphs, buttons) across cards. Builds on a...
canonical: https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Grid ideas: Creating a CSS subgrid utility class for rows | daily.dev
og:description: A walkthrough of building a reusable CSS subgrid utility class for aligning grid item rows (headings, images, paragraphs, buttons) across cards. Builds on a...
og:url: https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy
og:image: https://api.daily.dev/og/posts/uvabIrypY.png
og:image:alt: Grid ideas: Creating a CSS subgrid utility class for rows
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Grid ideas: Creating a CSS subgrid utility class for rows

**[utilitybend](https://daily.dev/sources/utilitybend)** · 8 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of building a reusable CSS subgrid utility class for aligning grid item rows (headings, images, paragraphs, buttons) across cards. Builds on a layered custom-properties grid utility technique from Stephanie Eckles, then extends it with grid-template-rows: subgrid. Covers a key limitation - container-type cannot be combined with subgrid on the same element - and offers two approaches to make the number of subgrid rows flexible: an inline style custom property, or a :has() selector chain that auto-detects child count up to a fixed limit.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://utilitybend.com/blog/grid-ideas-creating-a-css-subgrid-utility-class-for-rows>

## Questions this post answers

### why doesn't container-type work together with grid-template-rows: subgrid?

The container-type property can only be applied to grid containers, not grid items, while subgrid's grid-template-rows value is applied to a grid item to inherit the parent's row tracks. Because a single element cannot be both a subgrid item and a container in this way, the two properties conflict, so container queries and subgrid rows cannot be combined on the same element.

_Developers wiring up container queries alongside subgrid layouts can save this gotcha with daily.dev before it costs a debugging session._

### how can I make a CSS subgrid utility class handle a variable number of child elements?

Two approaches work: setting an inline custom property like style="--subgrid-rows: 4" on each grid section to declare the row count manually, or using a chain of :has(> :nth-child(n):last-child) selectors in CSS to automatically detect the child count up to a defined limit and set --subgrid-rows accordingly, which then feeds into grid-row: auto / span var(--subgrid-rows).

_Anyone building reusable subgrid utilities can bookmark patterns like this on daily.dev while refining their design system._

## Similar posts on daily.dev

- [CSS Subgrid](https://daily.dev/posts/css-subgrid-tzegbceau) · Flavio Copes · 4 upvotes · 0 comments
- [CSS Container Queries and Subgrid: Building Context-Aware Component Layouts](https://daily.dev/posts/css-container-queries-and-subgrid-building-context-aware-component-layouts-rddpjlqek) · SitePoint · 3 upvotes · 0 comments
- [Rebuilding FIFA Standings Layout with CSS Subgrid](https://daily.dev/posts/rebuilding-fifa-standings-layout-with-css-subgrid-didqlh9rj) · Ahmad Shadeed · 62 upvotes · 9 comments
- [My CSS layout strategy](https://daily.dev/posts/my-css-layout-strategy-lfp48ynra) · Blogging and all that Malarkey · 49 upvotes · 2 comments

---

Tags: [#css](https://daily.dev/tags/css), [#design-systems](https://daily.dev/tags/design-systems)

[View this post on daily.dev](https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Grid ideas: Creating a CSS subgrid utility class for rows","url":"https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy"},"datePublished":"2026-09-20T07:43:19.840Z","dateModified":"2026-09-20T07:43:41.862Z","description":"A walkthrough of building a reusable CSS subgrid utility class for aligning grid item rows (headings, images, paragraphs, buttons) across cards. Builds on a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/313b4c17caa6229055ee6c40a10c07e6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/313b4c17caa6229055ee6c40a10c07e6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"utilitybend","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"utilitybend","logo":"https://media.daily.dev/image/upload/s--hONVFSz4--/f_auto,q_auto/v1789890196/logos/utilitybend?_a=BAMAMicg0","url":"https://daily.dev/sources/utilitybend"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"css,design-systems","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"utilitybend","item":"https://daily.dev/sources/utilitybend"},{"@type":"ListItem","position":3,"name":"Grid ideas: Creating a CSS subgrid utility class for rows"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/grid-ideas-creating-a-css-subgrid-utility-class-for-rows-uvabirypy#faq","mainEntity":[{"@type":"Question","name":"why doesn't container-type work together with grid-template-rows: subgrid?","acceptedAnswer":{"@type":"Answer","text":"The container-type property can only be applied to grid containers, not grid items, while subgrid's grid-template-rows value is applied to a grid item to inherit the parent's row tracks. Because a single element cannot be both a subgrid item and a container in this way, the two properties conflict, so container queries and subgrid rows cannot be combined on the same element. Developers wiring up container queries alongside subgrid layouts can save this gotcha with daily.dev before it costs a debugging session."}},{"@type":"Question","name":"how can I make a CSS subgrid utility class handle a variable number of child elements?","acceptedAnswer":{"@type":"Answer","text":"Two approaches work: setting an inline custom property like style=\"--subgrid-rows: 4\" on each grid section to declare the row count manually, or using a chain of :has(\u003e :nth-child(n):last-child) selectors in CSS to automatically detect the child count up to a defined limit and set --subgrid-rows accordingly, which then feeds into grid-row: auto / span var(--subgrid-rows). Anyone building reusable subgrid utilities can bookmark patterns like this on daily.dev while refining their design system."}}]}
```

