Skip to the main content

All tutorials

A responsive card grid without media queries

  • Level: Beginner
  • About 5 minutes to read (an estimate from the word count: 829 words at 200 words a minute)
  • Last checked:

Lay out cards with auto-fit and minmax(), scale space and type with clamp(), and let a card adapt with a container query.

What you will be able to do

  • Write a grid whose column count follows the room available
  • Keep a column from overflowing a 360 px screen with min()
  • Scale spacing and type with clamp() between two limits
  • Make a card change its inside from the width of its own cell
  • Say when a media query is still the right tool

The auto-fit grid

A grid of cards needs one decision: how many columns fit? Instead of listing screen widths and a column count for each, describe the smallest width a column may have and let the browser count. In the grid below, one declaration does that.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
  gap: 1rem;
}
As many columns of at least 14rem as fit, sharing the leftover space equally.

Read it from the inside out. The code uses minmax(min(100%, 14rem), 1fr); to start with, read min(100%, 14rem) as plain 14rem. The next section explains why it is written this way. minmax(14rem, 1fr) says that a column is never narrower than 14rem and may grow to take an equal share of the free space. repeat(auto-fit, …) says to repeat that column as many times as fit in the row. When the container gets wider, a column is added. When it gets narrower, a column drops and the remaining cards grow.

The keyword auto-fit collapses any column that has no card in it. Its sibling auto-fill keeps those empty columns, so a short row of cards stays at the width it would have in a full row. Choose auto-fit when you want the cards to stretch. This difference is defined in the CSS Grid Layout specification, in its section on the repeat() notation; MDN's page on grid-template-columns does not spell it out.

HTML

<section class="fit-grid" aria-labelledby="fit-grid-title">
  <h2 class="fit-grid__title" id="fit-grid-title">Example guides</h2>
  <ul class="fit-grid__list">
    <li class="fit-grid__card"><h3>Lists</h3><p>Write a menu as a list of links.</p></li>
    <li class="fit-grid__card"><h3>Dialogs</h3><p>Open a native dialog as a modal.</p></li>
    <li class="fit-grid__card"><h3>Forms</h3><p>Let the browser check a value first.</p></li>
    <li class="fit-grid__card"><h3>Grids</h3><p>Let columns appear as room allows.</p></li>
    <li class="fit-grid__card"><h3>Themes</h3><p>Keep colours in custom properties.</p></li>
    <li class="fit-grid__card"><h3>Motion</h3><p>Respect a request for less motion.</p></li>
  </ul>
</section>

CSS

.fit-grid {
  --fg-bg: #ffffff;
  --fg-fg: #1b1f2a;
  --fg-line: #c9ced9;
  --fg-card: #f3f4f8;
  --fg-min: 14rem;
  padding: 1rem;
  color: var(--fg-fg);
  background: var(--fg-bg);
}
.fit-grid__title { margin: 0 0 1rem; font-size: 1.25rem; }
.fit-grid__list {
  display: grid;
  /* As many columns of at least 14rem as fit; min(100%, …) keeps one column from overflowing a narrow screen. */
  grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--fg-min)), 1fr));
  gap: 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.fit-grid__card {
  padding: 1rem;
  background: var(--fg-card);
  border: 1px solid var(--fg-line);
  border-radius: 8px;
}
.fit-grid__card h3 { margin: 0 0 0.25rem; font-size: 1.0625rem; }
.fit-grid__card p { margin: 0; }
@media (prefers-color-scheme: dark) {
  .fit-grid {
    --fg-bg: #12141c;
    --fg-fg: #eceef5;
    --fg-line: #444b5c;
    --fg-card: #1d2130;
  }
}

minmax() and the 360 px case

A fixed minimum causes one problem. If the container is narrower than the minimum, the column overflows it and the page scrolls sideways. The min() function removes the problem. min(100%, 14rem) is 14rem when there is room and the full container width when there is not.

The example grid shows this. At 360 px the container is narrower than two columns of 14rem, so the grid has one column. At 1440 px it has several. The check for this page measures both counts in Chrome: it reads the number of columns the browser has made, and does not trust the CSS.

clamp() for space and type

Spacing and type can follow the width of the window in the same way. clamp(smallest, preferred, largest) returns the preferred value, kept between the two limits. Make the preferred value a length that grows with the viewport width, such as 0.4rem + 1.8vw.

Write the preferred value as a rem part plus a vw part. The rem part follows the browser's text size, and only the vw part follows the window. The limits matter more than the middle: they make sure that the value is never too small to use or too large to look right.

.card {
  padding: clamp(0.75rem, 0.4rem + 1.8vw, 2rem);
}
.card h3 {
  font-size: clamp(1.125rem, 1rem + 0.8vw, 1.625rem);
}
Padding and heading size grow between a floor and a ceiling.

HTML

<section class="clamp-scale" aria-labelledby="clamp-scale-title">
  <h2 class="clamp-scale__title" id="clamp-scale-title">Space and type that scale</h2>
  <ul class="clamp-scale__list">
    <li class="clamp-scale__card"><h3 class="clamp-scale__heading">Small screens</h3><p>Padding and the heading start at their smallest value.</p></li>
    <li class="clamp-scale__card"><h3 class="clamp-scale__heading">Large screens</h3><p>They grow with the width, then stop at their largest value.</p></li>
    <li class="clamp-scale__card"><h3 class="clamp-scale__heading">Between</h3><p>In between, the value follows the width of the window.</p></li>
  </ul>
</section>

CSS

.clamp-scale {
  --cs-bg: #ffffff;
  --cs-fg: #1b1f2a;
  --cs-line: #c9ced9;
  --cs-card: #f3f4f8;
  /* clamp(smallest, preferred, largest): the preferred value grows with the viewport but never leaves the range. */
  --cs-space: clamp(0.75rem, 0.4rem + 1.8vw, 2rem);
  --cs-heading: clamp(1.125rem, 1rem + 0.8vw, 1.625rem);
  padding: var(--cs-space);
  color: var(--cs-fg);
  background: var(--cs-bg);
}
.clamp-scale__title { margin: 0 0 var(--cs-space); font-size: 1.25rem; }
.clamp-scale__list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
  gap: var(--cs-space);
  margin: 0;
  padding: 0;
  list-style: none;
}
.clamp-scale__card {
  padding: var(--cs-space);
  background: var(--cs-card);
  border: 1px solid var(--cs-line);
  border-radius: 8px;
}
.clamp-scale__heading { margin: 0 0 0.25rem; font-size: var(--cs-heading); }
.clamp-scale__card p { margin: 0; }
@media (prefers-color-scheme: dark) {
  .clamp-scale {
    --cs-bg: #12141c;
    --cs-fg: #eceef5;
    --cs-line: #444b5c;
    --cs-card: #1d2130;
  }
}

Because the values live in custom properties on the section, one change at the top rescales every card inside it.

A container query for the card

A media query asks about the window. A card does not know where it will be placed: it may fill a page or sit in a narrow column. A container query lets the card ask about the width of the element it sits in.

It takes two steps. First, mark the parent as a container with container-type: inline-size. Second, write an @container rule with a width condition. The rule styles the descendants of the container, not the container itself.

.cell { container-type: inline-size; }

@container (min-width: 28rem) {
  .card { grid-template-columns: 8rem 1fr; }
}
The card changes its inside when its cell is 28rem or wider.

The third example uses one card three times. The first cell spans the full width of the grid, so its card puts the picture beside the text. The other two cells are narrow, so their cards stack. The card has no knowledge of the window.

Marking an element as a container with inline-size means its width must not depend on what is inside it. That is why the grid cells, whose widths come from the grid, are the containers, and the cards are not.

HTML

<section class="card-box" aria-labelledby="card-box-title">
  <h2 class="card-box__title" id="card-box-title">One card, two widths</h2>
  <ul class="card-box__grid">
    <li class="card-box__cell card-box__cell--wide">
      <article class="card-box__card">
        <div class="card-box__art" aria-hidden="true"></div>
        <div class="card-box__text"><h3>Wide cell</h3><p>The card has room, so the picture sits beside the text.</p></div>
      </article>
    </li>
    <li class="card-box__cell">
      <article class="card-box__card">
        <div class="card-box__art" aria-hidden="true"></div>
        <div class="card-box__text"><h3>Narrow cell</h3><p>Same card, less room, so it stacks.</p></div>
      </article>
    </li>
    <li class="card-box__cell">
      <article class="card-box__card">
        <div class="card-box__art" aria-hidden="true"></div>
        <div class="card-box__text"><h3>Narrow cell</h3><p>The card reads its own width, not the window's.</p></div>
      </article>
    </li>
  </ul>
</section>

CSS

.card-box {
  --cb-bg: #ffffff;
  --cb-fg: #1b1f2a;
  --cb-line: #c9ced9;
  --cb-card: #f3f4f8;
  --cb-art: #3730a3;
  padding: 1rem;
  color: var(--cb-fg);
  background: var(--cb-bg);
}
.card-box__title { margin: 0 0 1rem; font-size: 1.25rem; }
.card-box__grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 12rem), 1fr));
  max-width: 44rem;
  gap: 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.card-box__cell--wide { grid-column: 1 / -1; }
/* The cell is the container: the card inside reads the width of its cell, not of the window. */
.card-box__cell { container-type: inline-size; }
.card-box__card {
  display: grid;
  gap: 0.75rem;
  padding: 1rem;
  background: var(--cb-card);
  border: 1px solid var(--cb-line);
  border-radius: 8px;
}
.card-box__art { height: 4rem; background: var(--cb-art); border-radius: 6px; }
.card-box__text h3 { margin: 0 0 0.25rem; font-size: 1.0625rem; }
.card-box__text p { margin: 0; }
@container (min-width: 28rem) {
  .card-box__card { grid-template-columns: 8rem 1fr; align-items: center; }
  .card-box__art { height: 6rem; }
}
@media (prefers-color-scheme: dark) {
  .card-box {
    --cb-bg: #12141c;
    --cb-fg: #eceef5;
    --cb-line: #444b5c;
    --cb-card: #1d2130;
    --cb-art: #b4b8ff;
  }
}

When a media query is still right

Do not remove media queries. They are the right tool when the question is about the window or the device, and not about a component. Examples are folding a site's main menu, placing a sidebar next to the content, and the user preferences prefers-color-scheme and prefers-reduced-motion. Use a container query for a component and a media query for the page.

All examples on this page passed their checks in Chrome 143 on 3 December 2026. They were not tested in other browsers.

References

These snippets in the gallery use the same techniques: