Kelly Mears

Container Query

Styling based on the size of an element's container rather than the viewport.

Design & Interface2 min read214 words11 out · 8 in
also calledContainer queries

A container query applies styles based on the dimensions of a designated ancestor rather than the viewport. It is the answer to a problem that viewport-based Responsive Breakpoints cannot solve: a component's correct layout depends on the space it has been given, not on the size of the window.

The distinction becomes urgent as soon as anything else on the page can take space away. A navigation bar sized by viewport breakpoints will crowd and overflow the moment a panel opens beside it, because the viewport did not change but the available width did. Every editing interface that pushes content aside hits this, and it is a container problem across all of that interface's content, not a bug in the one component where it was noticed.

Container queries require declaring a containment context on the ancestor, which has layout implications of its own — a size container cannot be sized by its contents in the queried dimension. That constraint is what delayed the feature for years and is why retrofitting it into an existing layout is more than a find-and-replace.

Where they are not yet practical, the honest fallback is to make components tolerant of narrowness rather than to add more viewport breakpoints, which encode an assumption about available space that is simply wrong.

See also10

UI Primitive

The small set of typed components through which all interface markup is expressed.

Design & Interface12 connections

Utility-First CSS

Composing styles from many small single-purpose classes rather than authoring semantic rules.

Design & Interface15 connections

Headless Component

A component supplying behavior and accessibility with no styling of its own.

Design & Interface16 connections

Design Token

Named design decisions — color, spacing, radius, type — stored as data so they can be shared and re-themed.

Design & Interface21 connections

Typographic Scale

A constrained set of type sizes and their paired line heights and spacing.

Design & Interface17 connections

Design System

A shared vocabulary of tokens, primitives, and rules that makes independent work converge.

Design & Interface22 connections

Silent Failure

A failure that produces no signal, so the absence of an error reads as success.

Method73 connections

Cascading Style Sheets

The language that describes how web pages look, proposed in 1994 to separate presentation from structure, nearly sunk by inconsistent browser support, and grown since into a layout and animation system that does work once reserved for scripts.

Web Platform11 connections

CSS Grid

The CSS layout mode for rows and columns at once, which brought the designer's layout grid into the browser and lets responsive layouts adapt with no breakpoints at all.

Design & Interface10 connections

Flexbox

The CSS layout mode for arranging items along one axis, distributing space and aligning them without floats or hacks, which finally made vertical centering trivial and powers most component layouts.

Design & Interface10 connections

Linked from8