Recipe
Container queries adoption
Replace media queries with container queries for component-driven responsive design.
Why
Media queries tie layout to the viewport. Container queries let a component respond to its own available width, making it truly reusable across any parent context.
How
- Define a containment context — set
container-type: inline-sizeon the parent. - Name the container — use
container-namefor scoped queries. - Write container queries —
@container (min-width: 400px)replaces@media. - Audit existing media queries — migrate component-level breakpoints first; leave page-layout queries as-is.
Gotchas
- Container queries cannot size the container itself — avoid circular dependencies.
- Height queries (
block-size) are still limited in some browsers. - Containment clips overflow by default; add
overflow: visibleif needed.
Baseline: Container queries are Baseline Widely Available as of 2024. Safe to ship without a polyfill for all modern browsers.