# CSS Container Queries (@container) vs JS ResizeObserver Pattern for Adaptive Layout

- **Canonical URL:** https://frontend.rocks/vs/css-container-queries-vs-js-resize-observer-pattern/
- **Relation:** Replaces
- **Last Verified:** 2026-11-15
- **Verdict:** Eliminates ResizeObserver + JS class toggling to adapt components to their container.

## Comparison Matrix

| Dimension | CSS Container Queries (@container) | JS ResizeObserver Pattern for Adaptive Layout |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Superseded |
| **Domain** | CSS & Layout | CSS & Layout |
| **Mental Model** | CSS queries evaluating an explicit parent container's dimensions or styles instead of the global viewport, resolved natively during Layout. | Legacy pattern using ResizeObserver and JS state to toggle CSS classes based on container width. Superseded by @container. |
| **Prerequisites** | None | None |
| **When Not to Use** | When not to use @container: For macro page layout (global navigation, main page grid), keep viewport @media queries. | When not to use ResizeObserver: Stop using it for responsive CSS styling; reserve it for <canvas> or WebGL viewport resizing. |

## CSS Container Queries (@container) — Under the Hood
1. `container-type: inline-size` enables inline-axis size containment: the container's width no longer depends on its children.
2. This containment breaks the circular sizing dependency that historically blocked parent queries (and forced teams into the [JS ResizeObserver Pattern](/concepts/js-resize-observer-pattern)).
3. During the Layout pass of the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline), the engine resolves container size, evaluates `@container` rules, and resolves descendant styles and geometry in a single top-down pass.

```css
.card-slot {
  container-type: inline-size;
  container-name: card;
}

@container card (min-width: 420px) {
  .card {
    display: grid;
    grid-template-columns: 160px 1fr;
    gap: 1rem;
  }
}
```

## JS ResizeObserver Pattern for Adaptive Layout — Under the Hood
1. The engine executes the initial Layout pass of the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline) using default classes.
2. `ResizeObserver` invokes the JS callback right before Paint with the measured `contentRect`.
3. Mutating component state (`setState` / `classList.toggle`) invalidates style and forces a second Layout calculation on the next frame.

```js
// Legacy pattern: measures box after Layout, then dirties Layout again
const ro = new ResizeObserver(([entry]) => {
  const isWide = entry.contentRect.width >= 420;
  card.classList.toggle('card--wide', isWide);
});
ro.observe(cardSlot);
```

## Interactive Labs & Dissections
- [CSS Container Queries (@container) Lab](https://frontend.rocks/concepts/css-container-queries/#lab) ([Markdown](https://frontend.rocks/concepts/css-container-queries.md))
- [JS ResizeObserver Pattern for Adaptive Layout Lab](https://frontend.rocks/concepts/js-resize-observer-pattern/#lab) ([Markdown](https://frontend.rocks/concepts/js-resize-observer-pattern.md))

## Primary Sources
- https://drafts.csswg.org/css-contain-3/
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_containment/Container_queries
- https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
