CSS Container Query Card Responsive

Use container queries for component-level responsive design independent of viewport.

CssData Fetching

Code

csssnippet
21 lines
.card-container {
  container-type: inline-size;
}
.card {
  display: flex;
  flex-direction: column;
}
@container (min-width: 400px) {
  .card {
    flex-direction: row;
    gap: 1rem;
  }
  .card img {
    max-width: 200px;
  }
}
@container (max-width: 200px) {
  .card {
    font-size: 0.875rem;
  }
}

Inside the snippet

display: flex distributes the children along one axis, and the alignment properties decide how leftovers are handled.

@container queries size the component against its container instead of the viewport.

gap spaces grid/flex items without margins on the children.

@container queries drive the responsive behavior from the parent element’s size rather than the viewport.

The rules target .card-container, container-type: inline-size; } .card, .card, flex-direction: row; gap: 1rem; } .card img, max-width: 200px; } } @container (max-width: 200px) — each selector names the element it restyles, in source order.

Interactive Tools for This Topic

FAQ

Which elements does this stylesheet target?
The rules bind to `.card-container`, `container-type: inline-size; } .card`, `.card`, `flex-direction: row; gap: 1rem; } .card img`; when two rules tie in specificity, the one written later in the file wins.