Container Style Queries Hit Baseline: CSS Gains Context-Driven Modifiers
Container style queries are Baseline Newly Available: layouts can now flip card density, grid direction and overflow behavior by setting one custom property, no child selectors required. Kevin's patterns show where they beat modifier classes.
- AuthorPriya Raman
- ChannelDesign Systems
- Filed
- Read4 min

- Container style queries are now Baseline Newly Available in CSS
- Style queries inspect only custom properties today; regular properties remain spec-only and unsupported
- The article adapts a lesson from Writing Resilient CSS, a module of Kevin's course CSS Demystified, 32% off until December 2, 2026
- A media query above 700px width switches sidebar cards to compact density by setting one custom property
- The upcoming if() function will read custom properties on the element itself, but styles one property per use
Container style queries — the @container variant that reacts to a custom property's value instead of a container's width — are now Baseline Newly Available, and they change how conditional CSS gets written. The mechanism reads one line: @container style(--theme: high-contrast) { ... } means "if the nearest ancestor carries --theme: high-contrast, apply these styles." That single rule is the whole API, and its consequences ripple through how cards, grids, and page sections talk to each other.
The piece comes from Kevin's course CSS Demystified, adapted from a lesson in its newest module, Writing Resilient CSS, which covers custom-property configuration, math functions, relative colors, :has(), and style queries in reusable patterns. The course runs 32% off until December 2, 2026.
What replaces the modifier class?
Developers have styled variants for years with modifier classes and attribute selectors — BEM's .button--accent, CUBECSS's .toast[data-type="error"]. Kevin opens with a card grid: .card--compact sets padding: .75rem and gap: .5rem, and a template loop applies it to every card in a sidebar. That works, until the design team objects. Compacted cards lower on the page lose the padding that created visual separation between sections, so the variant stops aiding hierarchy and starts creating inconsistency.
You could patch that with a Resize Observer. Style queries offer the CSS-native route: a media query, container query, :has() selector, or parent section sets one custom property, and the styles written once switch on. Kevin shows the split explicitly:
@media (width > 700px) {
.layout {
grid-template-columns: 1fr 18rem;
.sidebar { --card-density: compact; }
}
}
Anything that can set a custom property becomes a trigger. That includes CMS authors, who can pick an option that sets one property value on a page section and let the styles kick in.
What are the constraints?
Two technical limits matter. First, every element is a style container by default — no container-type declaration needed, unlike size queries. Second, queries only work with custom properties today; querying regular properties like style(display: flex) sits in the spec but ships nowhere.
The sharper gotcha: elements can't query themselves. A --card-layout set on .card and queried in the same rule does nothing, because style queries, like size queries, inspect an ancestor. Kevin calls this "something that always tends to catch people out," though he doesn't find it limiting in practice. The upcoming if() function will see custom properties on the element itself, but it applies to a single property, where a style query can style many.
How far does it scale beyond cards?
Kevin's second case is an overflow grid he uses often: grid-auto-flow: column, grid-auto-columns: max(40%, 25ch), horizontal scrolling. The flaw — it always overflows, even when three children barely spill a third card out, which reads as awkward rather than intentional. One fix combines :has() with a size query: when fewer than five children exist and the container exceeds 75ch, grid-auto-columns: 1fr lets items fill the space.
But bigger contextual changes need more. Picture a hero that splits into two columns above 40rem — heading on one side, featured articles in an overflow grid on the other. Horizontal scrolling inside a narrow split feels wrong; Kevin wants vertical scrolling there. The split sets a configuration:
.split {
@container main (width > 40rem) {
--overflow-grid-direction: row;
grid-template-columns: repeat(2, 1fr);
}
}
The naive result disappoints: the grid stays sized for horizontal scrolling, producing "a tall, awkward column, and no overflow at all." So the grid listens for the change itself:
.overflow-grid {
@container style(--overflow-grid-direction: row) {
grid-template-columns: 1fr;
max-block-size: 50vb;
}
}
The split never styles the overflow grid's children directly — possible, Kevin admits, but "awkward" and a path to fragile codebases. Instead the layout tweaks settings, and the component decides how to respond.
Do modifier classes die?
No, and Kevin says so plainly. A style query can't inspect the element it styles, so a simple accent button or pill shape still belongs to a modifier. A regular media query, container query, or an intrinsic pattern may also do the job fine.
Style queries earn their place when the condition and the styles must live in different places, or when one condition drives several components at once — the split case. That, for Kevin, is the definition of resilient CSS: components that keep working, adapting sensibly, wherever they end up. The caveat stands, though — custom properties only, ancestor queries only, and if() still on the way to fill the gaps.
Original: cssdemystified.com
More from Priya Raman
Show full bio
Market editor covering technology and product news in Product and interaction design, design systems at Interface Wire.
161 articles