Modern CSS: Container Queries, :has() & Cascade Layers
By: Md Tarek · 17 Oct 2026
Executive Summary & Key Takeaways
- Container queries answer "how wide is my parent?" instead of "how wide is the viewport?" — the missing primitive for truly reusable components that adapt to wherever they are placed.
- `:has()` is the "parent selector" we always wanted, letting you style an element based on what it contains:
form:has(input:invalid)orfigure:has(img). - Cascade layers (`@layer`) give you control over specificity — wrap your resets, defaults, and overrides in layers so later layers always win without specificity hacks.
- These features are baseline now, so they are safe for production without heavy transpilation.
1. Container Queries
Container queries let a component change its layout based on the container's size, not the viewport. This is the difference between a card component that works in a sidebar and a full-width grid without bespoke variants.
.card-grid { container-type: inline-size; }
@container (min-width: 400px) {
.card { grid-template-columns: 1fr 1fr; }
}2. The :has() Selector
:has() finally lets you select a parent based on its children. Form validation styling becomes declarative:
field:has(input:invalid) { border-color: var(--danger); }
.card:has(img) { padding: 0; }Use it for state-driven UI that previously needed JavaScript class toggles.
3. Cascade Layers
Layers resolve the age-old fight against specificity. Define your styles in priority order, and later layers override earlier ones regardless of selector specificity:
@layer reset, base, components, utilities;
Third-party CSS goes in an early layer; your overrides in a later one — no more !important arms races.
4. Choosing the Right Tool
| Problem | Tool | |---|---| | component adapts to container | container query | | style parent by child state | :has() | | control cascade priority | @layer | | page adapts to device | media query (still) | | theme values | custom properties |
FAQ
Do I still need media queries? Yes — for device-level concerns like orientation, coarse pointers, and reduced motion. Container queries handle component-level layout; media queries handle the viewport.
