Responsive Grid and Subgrid

W3C CSS Snapshot 2026

Build columns that adapt to the available width, then line up a nested grid with its parent tracks.

On this page

Let the grid decide how many columns fit

repeat(auto-fit, ...) fills the row with as many tracks as fit and collapses unused tracks. minmax sets a minimum and maximum track size. min(100%, 150px) lets the minimum fit very narrow screens.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Let the grid decide how many columns fit</title>
  <style>
    .cards { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); }
    .cards > div { background: #d6efea; padding: 8px; }
  </style>
</head>
<body>
<div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>

What to expect Resize the browser. The cards use fewer columns as the available width shrinks.

Compare auto-fill and auto-fit with two cards

Both keywords repeat as many tracks as fit. auto-fill keeps empty tracks. auto-fit collapses empty tracks and their adjacent gutters, so occupied flexible tracks can use the space. The difference shows up when the container can fit more tracks than there are items.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare auto-fill and auto-fit with two cards</title>
  <style>
.layout { display: grid; width: 360px; max-width: 100%; gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 100px), 1fr)); }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
  </style>
</head>
<body>
<div class="layout"><div>Pen</div><div>Notebook</div></div>
</body>
</html>

What to expect At 360px, three 100px tracks fit. Two cards occupy the first two. Change auto-fill to auto-fit and they expand across the row.

Align nested rows with their parent using subgrid

A nested grid normally sizes its own tracks. subgrid reuses the parent tracks on the chosen axis over the lines the child spans. Here each record spans both parent columns. The widest label sets one shared label width. You can inherit rows with grid-template-rows: subgrid as well.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Align nested rows with their parent using subgrid</title>
  <style>
.layout { display: grid; grid-template-columns: max-content 1fr; gap: 8px; }
.record { display: grid; grid-column: 1 / -1; grid-template-columns: subgrid; }
.record > * { padding: 4px; background: #d6efea; }
  </style>
</head>
<body>
<div class="layout">
  <div class="record"><b>Name</b><span>Notebook</span></div>
  <div class="record"><b>Stock status</b><span>Available</span></div>
</div>
</body>
</html>

What to expect Both values start on the same line. Replace subgrid with max-content 1fr on .record and each label gets its own width.

Official references