CSS Container Queries

Implement responsive components using CSS container queries that adapt based on parent container width not viewport.

CssConfig Management

Code

.sidebar {
  container-type: inline-size;
  container-name: sidebar;
}

@container sidebar (min-width: 400px) {
  .card {
    grid-template-columns: 1fr 1fr;
  }
}

CSS Container Queries

Implement responsive components using CSS container queries that adapt based on parent container width not viewport.

Code

.sidebar {
  container-type: inline-size;
  container-name: sidebar;
}

@container sidebar (min-width: 400px) {
  .card {
    grid-template-columns: 1fr 1fr;
  }
}

Explanation

A component reflows from the width of its parent container rather than the viewport, so the same card adapts inside a narrow sidebar and a wide main column. Expected behavior: the @container rules apply once the nearest container meets the size condition. Constraint: the ancestor must be declared a container with container-type, otherwise the query never matches.

Interactive Tools for This Topic