Container Queries

suggest change

Media queries respond to the viewport or device. Container queries let a reusable component respond to the space provided by an ancestor.

First establish a query container:

.card-list {
  container: cards / inline-size;
}

Then query its inline size:

.card {
  display: grid;
  gap: 1rem;
}

@container cards (inline-size >= 30rem) {
  .card {
    grid-template-columns: 10rem minmax(0, 1fr);
    align-items: start;
  }
}

Naming containers avoids accidentally selecting a nearer container when components are nested. Use container-type: inline-size without a name when that ambiguity cannot occur.

Container query units are relative to the selected query container. For example, 1cqi is one percent of its inline size:

.card__title {
  font-size: clamp(1.25rem, 1rem + 2cqi, 2rem);
}

Always provide a usable default outside the query. If no eligible container exists, the conditional rules do not apply.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents