Back to CSS Universe

intermediate

Container Queries

Component-driven responsiveness using container size instead of viewport.

container-queriesresponsive

Learning checklist

  • @container rules
  • Container units (cqw/cqh)
  • Style queries vs size queries

Definition

Component-driven responsiveness using container size instead of viewport.

Syntax

@container rules

Example

Use the visualizer below to apply these properties and see the result live.

Deep dive

Why this matters

Principles, mental models, and debugging clues tailored to the topic.

Visual intuition

Each property is paired with motion or color feedback so you build muscle memory, not just recall syntax.

Copy-ready CSS

Controls emit exact CSS you can copy. Use it as a starter token or paste into DevTools for instant validation.

Performance notes

Learn where reflow, repaint, or compositing occurs so you avoid jank and choose GPU-friendly paths.

Accessibility first

Focus-visible, reduced-motion, and contrast tips are woven into every lab to keep experiences inclusive.

Practice

Try recreating a UI card using this topic. Toggle between dark and light modes to validate token contrast and responsiveness.

Tip: use DevTools overlays (grid/flex) plus the copy CSS button.

Interactive Lab

Container Queries

Simulate a component that changes style based on its container width.

Container query: min-width 320px

Try these

  • Increase the min width and note when the style flips.
  • Compare to viewport media queries: this is parent-sized.
  • Use container-type and @container together to scope styles.
Resize me via the range; above 320px, apply the container query style.

Container queries react to parent size, not viewport. Use container-type and @container rules.

CSS
.card {
  container-type: inline-size;
}
@container (min-width: 320px) {
  .card { background: #fbbf24; }
}
HTML
<div class="card">Resize parent; above 320px, container query applies.</div>

Related topics

Keep exploring adjacent concepts.

foundation

Responsive Design

Fluid design systems built with constraints, clamps, and progressive enhancement.

View page

foundation

Media Queries

Craft media queries for viewport, accessibility preferences, and device capabilities.

View page

foundation

Units (px, em, rem, vw, vh, fr, %)

Choose the right units for sizing, spacing, and layout grids.

View page