ai CSS: container query 2026-06-06

xtodo

ai generated

CSS Container Queries let you apply styles to elements based on the size (or other features) of their containing element, rather than the viewport. This makes truly reusable, component-driven responsive design possible.

Unlike media queries (@media), which are global, container queries are local to a specific parent/container.

Why Container Queries Matter

Browser Support: Baseline (widely available) across Chrome/Edge 105+, Firefox 110+, Safari 16+ (since ~2023). Fully production-ready in 2026.

Core Concepts

  1. Containment Context — You explicitly mark an element as a container using the container-type property.
  2. Container Query — Use the @container at-rule to apply styles when the container meets certain conditions.
  3. Optional Naming — Give containers names with container-name for targeted queries.

Step 1: Define a Container

.card-container {
  container-type: inline-size; /* Most common and performant */
}

/* Or for both width and height (more expensive) */
.full-size-container {
  container-type: size;
}

Values for container-type:

Step 2: Write a Container Query

@container (min-width: 400px) {
  .card {
    display: flex;
    flex-direction: row;
    gap: 1rem;
  }

  .card img {
    width: 150px;
  }
}

This applies styles to .card (and descendants) when its nearest container with container-type is at least 400px wide.

Example: Responsive Card Component

HTML

<div class="card-container">
  <article class="card">
    <img src="..." alt="">
    <div class="content">
      <h2>Title</h2>
      <p>Description...</p>
    </div>
  </article>
</div>

CSS

.card-container {
  container-type: inline-size;
  container-name: card; /* Optional but useful */
}

.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Narrow card (default) */
@container card (max-width: 350px) {
  .card {
    font-size: 0.9rem;
  }
}

/* Wide card */
@container card (min-width: 400px) {
  .card {
    flex-direction: row;
  }

  .card img {
    flex: 0 0 180px;
  }
}

Naming Containers

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

.main-content {
  container-name: main;
  container-type: inline-size;
}

/* Target specific container */
@container sidebar (max-width: 300px) {
  .widget { /* styles for narrow sidebar */ }
}

@container main (min-width: 800px) {
  .widget { /* styles for wide main area */ }
}

You can combine name + condition or omit the name (uses nearest ancestor container).

Container Query Length Units (cqi, cqmin, etc.)

These are relative to the container:

Example

@container (min-width: 30rem) {
  h2 {
    font-size: clamp(1.5rem, 5cqi, 3rem);
  }
}

This scales typography beautifully based on the container.

Logical Operators

Just like media queries:

@container (width > 400px) and (height > 500px) { ... }

@container not (max-width: 300px) { ... }

@container (orientation: portrait) { ... }

Style Queries (Advanced)

Query based on computed styles (e.g., custom properties):

.container {
  container-type: style;
  --theme: light;
}

@container style(--theme: dark) {
  .card { background: #333; color: white; }
}

Support is still evolving (stronger in 2026).

Best Practices

Common Patterns

Responsive Typography

@container (min-width: 20rem) {
  h1 { font-size: 2.5rem; }
}

Component Variants

With CSS Nesting (modern browsers)

.card-container {
  container-type: inline-size;

  & .card {
    /* base styles */

    @container (min-width: 500px) {
      /* wide styles */
    }
  }
}

Limitations & Gotchas

Further Reading

Container queries are one of the most exciting additions to CSS in years. They shift responsive design from "page level" to "component level" and pair beautifully with modern layout techniques. Start experimenting with simple cards and lists — you'll quickly see the power!