Перейти к основному содержимому
← 🎨 CSS as a design system·Модуль B3 · Урок 10
ЗаданиеBuild a card row where each card has title + body + footer, all aligned across cards via subgrid. .cards: display: grid, grid-template-columns: repeat(3, 1fr), grid-template-rows: auto auto auto, gap: 16px. Each .card: display: grid, grid-template-rows: subgrid, grid-row: span 3, gap: 8px, padding: 12px, background: #eef.

подсетка: дочерние сетки, которые совпадают с родительскими

100 XP7 min
Теория

When a nested grid needs to align with its parent

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto auto auto;   /* title row, body row, footer row */
  gap: 16px;
}

.card {
  display: grid;
  grid-template-rows: subgrid;          /* INHERIT row tracks from parent */
  grid-row: span 3;                     /* this card spans all 3 parent rows */
  gap: 12px;
}

Without subgrid, each card has its OWN row sizes — title/body/footer in card 1 doesn't align with card 2. With subgrid, every card's title row lines up with every other card's title row. Same for body and footer.

What this fixes

Before subgrid (2022), the canonical solution was to put all titles in one grid row, all bodies in another, all footers in another — and use display: contents to flatten the DOM. Hacky.

grid-template-columns: subgrid works the same way for columns.

Browser support (2026)

  • Firefox: long-standing.
  • Safari: shipped 2023.
  • Chrome / Edge: shipped late 2023.

Treat as widely supported in 2026. No polyfill — older browsers fall back to non-subgrid layout.

When NOT to use subgrid

If your "alignment across cards" need can be solved by giving every card a fixed-height title or a flexbox spacer, do that. Subgrid is for cases where the content height varies but you want rows to align across siblings.

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.