CSS Responsive & Modern Features

Subgrid

Create perfectly aligned layouts with CSS subgrid. Share grid tracks between parent and child elements for consistent alignment.

What is CSS subgrid?

subgrid lets a nested grid reuse its parent grid's row or column tracks instead of defining its own. It's ideal for card lists where titles, text and buttons must line up across every card, whatever each card's content length.

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.card {
  display: grid;
  grid-row: span 3;               /* title, body, footer */
  grid-template-rows: subgrid;    /* align with the siblings */
}

Interactive Subgrid Demo

Code Editor

<div class="cards">
  <article class="card"><h3>Short</h3><p>One line.</p><button>Buy</button></article>
  <article class="card"><h3>A much longer title that wraps</h3><p>Text.</p><button>Buy</button></article>
  <article class="card"><h3>Mid title</h3><p>Some longer description text here.</p><button>Buy</button></article>
</div>

Preview

Subgrid Fundamentals

Understanding Subgrid

/* Parent grid container */ .parent-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto auto auto;
  gap: 1rem;
}

/* Child grid item using subgrid */ .child-grid {
  display: grid;
  grid-template-rows: subgrid; /* Inherit parent rows */   grid-row: span 3; /* Span across rows */ }

/* Alternative: columns subgrid */ .child-columns {
  display: grid;
  grid-template-columns: subgrid; /* Inherit parent columns */   grid-column: span 2; /* Span across columns */ }

/* Both directions */ .child-both {
  display: grid;
  grid-template: subgrid / subgrid;
  grid-area: 1 / 1 / 4 / 4; /* Span entire area */ }

Subgrid allows child elements to participate in the parent's grid, sharing track sizing.

Basic Implementation

/* Card layout with aligned content */ .card-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
}

.card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 4; /* image, title, content, button */   background: white;
  border-radius: 8px;
  overflow: hidden;
}

/* Individual card elements */ .card-image { /* Row 1 */ }
.card-title { /* Row 2 */ }
.card-content { /* Row 3 */ }
.card-button { /* Row 4 */ }

/* Content automatically aligns across all cards */

Perfect for card layouts where content needs consistent alignment

Grid Track Sharing

/* Parent defines the grid structure */ .layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

/* Header spans all columns */ .header {
  grid-column: 1 / -1;
}

/* Main content uses subgrid */ .main {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  grid-row: 2;
}

/* Sidebar, content, and aside align perfectly */ .sidebar { grid-column: 1; }
.content { grid-column: 2; }
.aside { grid-column: 3; }

.footer {
  grid-column: 1 / -1;
}

Enable complex layouts with perfect alignment across components

Practical Applications

Form Layouts

/* Multi-step form with aligned sections */ .form-container {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto auto auto auto;
  gap: 1rem;
}

.form-section {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  padding: 1rem;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

/* Labels and inputs align across sections */ .form-label {
  grid-column: 1;
  font-weight: 600;
  align-self: center;
}

.form-input {
  grid-column: 2;
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
}

/* Perfect alignment without manual spacing */

Create complex forms with perfect label and input alignment.

Data Tables

/* Table-like layout with subgrid */ .data-table {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr 1fr;
  gap: 1px;
  background: #e5e7eb;
}

.table-header {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  background: #f3f4f6;
  font-weight: 600;
}

.table-row {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  background: white;
}

.table-cell {
  padding: 0.75rem;
  align-content: center;
}

/* Responsive column adjustments */ @media (max-width: 768px) {
  .data-table {
    grid-template-columns: 1fr 1fr;
  }
}

Build responsive data tables with consistent column alignment

Product Grids

/* E-commerce product grid */ .product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
}

.product-card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 5; /* image, title, description, price, button */   background: white;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.product-image {
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.product-title {
  padding: 1rem 1rem 0.5rem;
  font-weight: 600;
}

.product-description {
  padding: 0 1rem;
  color: #6b7280;
}

.product-price {
  padding: 0.5rem 1rem;
  font-size: 1.25rem;
  font-weight: 700;
  color: #059669;
}

.product-button {
  margin: 1rem;
  padding: 0.75rem;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
}

Perfect product layouts with aligned pricing and actions

Advanced Techniques

Nested Subgrids

/* Multi-level grid hierarchy */ .page-layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

.main-content {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  grid-row: 2;
}

.content-area {
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-column: 2;
  padding: 2rem;
}

.article-section {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: 1 / -1;
}

/* Deep nesting maintains alignment */ .nested-component {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
}

Create complex hierarchical layouts with consistent alignment at every level.

Browser Support & Fallbacks

/* Feature detection and fallbacks */ .grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

/* Fallback for browsers without subgrid */ .grid-item {
  display: flex;
  flex-direction: column;
  background: white;
  border-radius: 8px;
}

.item-content {
  flex: 1;
}

.item-footer {
  margin-top: auto;
}

/* Enhanced with subgrid when supported */ @supports (grid-template-rows: subgrid) {
  .grid-item {
    display: grid;
    grid-template-rows: subgrid;
    grid-row: span 4;
  }
  
  .item-content,
  .item-footer {
    flex: initial;
    margin: 0;
  }
}

Progressive enhancement ensures good experiences in all browsers

Performance Optimization

/* Efficient subgrid implementations */ .optimized-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
  /* Use contain for performance */   contain: layout style;
}

.grid-card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
  /* Contain layout changes */   contain: layout style paint;
}

/* Minimize reflows with fixed spans */ .fixed-height-card {
  grid-row: span 4; /* Fixed span prevents reflow */ }

/* Use will-change for animated grids */ .animated-grid {
  will-change: grid-template-columns;
}

/* Optimize for large datasets */ .virtual-grid {
  content-visibility: auto;
  contain-intrinsic-size: 200px;
}

Optimize performance with containment and efficient grid patterns

Best Practices & Browser Support

Best Practices

  • • Use subgrid for content that needs alignment across items
  • • Always provide fallbacks for older browsers
  • • Keep grid spans consistent for predictable layouts
  • • Test with varying content lengths and sizes
  • • Use @supports for progressive enhancement
  • • Consider performance with large grid structures

Browser Support

  • • Firefox 71+ (December 2019)
  • • Safari 16+ (September 2022)
  • • Chrome/Edge: In development
  • • Limited but growing support (~30%)
  • • Use feature queries for detection
  • • Progressive enhancement essential

CSS Subgrid — step by step

1 / 4
1

What is Subgrid?

Subgrid lets a nested grid item inherit its parent's track definitions — grid-template-columns: subgrid makes children align to the outer grid columns.

1 of 4
🔍Subgrid Solves the Card Alignment Problem
The classic card grid problem: different-length content breaks vertical alignment across cards. Subgrid lets each card's title, description, and footer rows align to a shared row track across ALL cards.

🎯 Quick Check

What does grid-template-columns: subgrid do to a nested grid item?

CSS Subgrid: summary

Key points

  • The child must itself be a grid item that spans the tracks it wants to inherit.
  • You can subgrid rows, columns or both independently.
  • Gaps are inherited from the parent but can be overridden on the subgrid.
  • Supported in all modern browsers since 2023.

Frequently asked questions

›When should I use subgrid?

When the parts of sibling components, such as card headers or prices, need to line up across a row even though their content lengths differ.

›Is subgrid supported?

Yes. Chrome, Edge, Firefox and Safari all support subgrid in their current versions.