JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicscssContainer Queries (@container)
PrevNext
css
advanced
12 min read

Container Queries (@container)

components
container-queries
container-units
containment
layout
modern-css
responsive

Container queries let components adapt their styling based on the size of their parent container rather than the viewport — enabling truly reusable responsive components that work in any layout context.

Key Points

1Component-Level Responsiveness

Container queries style elements based on their parent container's size instead of the viewport, making components truly reusable across different layout contexts.

2Containment Context

A parent element must declare container-type (inline-size or size) before children can query its dimensions using @container rules.

3Container Query Units

New units like cqw, cqh, cqi, and cqb are relative to the query container's dimensions, enabling fluid sizing that scales with the component's available space.

4Complementary to Media Queries

Media queries handle page-level layout decisions, while container queries handle component-level responsiveness — both are needed in modern CSS architecture.

What You'll Learn

  • Explain why media queries are insufficient for component-level responsive design
  • Set up a containment context and write @container queries for responsive components
  • Use container query units (cqw, cqi) for fluid component sizing
  • Decide when to use container queries versus media queries in a project

Deep Dive

Container queries are one of the most significant additions to CSS in recent years. They solve a fundamental limitation of media queries and represent a paradigm shift from responsive pages to responsive components.

The Media Query Limitation

Media queries respond to the viewport width. A card component styled to stack vertically below 600px viewport width works fine on narrow screens. But place that same card in a 300px sidebar on a 1440px desktop, and the media query sees the wide viewport — the card stays in its wide layout even though its container is narrow. Media queries cannot solve this because they have no awareness of individual container sizes.

How Container Queries Work

Container queries require two steps:

  1. Declare a containment context on the parent element:
CSS
.sidebar {
  container-type: inline-size;
  container-name: sidebar; /* optional, for targeting */
}
  1. Query the container from child styles:
CSS
@container sidebar (max-width: 400px) {
  .card { flex-direction: column; }
}

If no container name is specified, @container queries the nearest ancestor with containment.

Container Types

  • inline-size — Enables queries on the inline (horizontal) dimension. This is the most common and safest option.
  • size — Enables queries on both inline and block dimensions. Requires the browser to fully contain the element in both axes, which can cause layout issues if the element needs to size based on its children's height.
  • normal — Default; no containment. The element cannot be queried but can still be a named container for style queries.

Container Query Units

Container queries introduce new units relative to the query container:

  • cqw / cqh — 1% of the container's width/height
  • cqi / cqb — 1% of the container's inline/block size
  • cqmin / cqmax — The smaller/larger of cqi and cqb

These units enable fluid typography and spacing that scales with the container, not the viewport.

Style Queries (Experimental)

Beyond size, the specification includes style queries that let you check the computed value of custom properties on a container:

CSS
@container style(--theme: dark) {
  .card { background: #1a1a1a; }
}

Style queries are still gaining browser support but open powerful possibilities for theming and variant systems.

Container Queries vs Media Queries

Media queries are still appropriate for page-level layout decisions (overall page structure, navigation patterns, global font sizes). Container queries are for component-level responsiveness — making components truly portable and reusable across different layout contexts.

The mental model shift: media queries make pages responsive; container queries make components responsive.

Real-World Use Cases

  • Card components that switch between horizontal and vertical layouts based on available space
  • Navigation menus that collapse in narrow containers
  • Data tables that switch to stacked layouts in small containers
  • Design system components that work in any layout context without modification

Browser Support

Size container queries are supported in all modern browsers since February 2023 (Chrome 105, Firefox 110, Safari 16). Style queries have partial support. For older browsers, components fall back to their default (widest) layout.

Fun Fact

Container queries were the most requested CSS feature for over a decade. The main technical blocker was circularity — if a child's size depends on the parent, and the parent's size depends on the child, you get infinite loops. The containment specification solved this by requiring explicit size containment on the parent.

Learn These First

Responsive Design Fundamentals

beginner

Flexbox Layout

beginner

Continue Learning

Cascade Layers (@layer)

advanced

CSS Grid Layout

intermediate

Practice What You Learned

What are CSS container queries and how do they improve on media queries?
senior
container-queries
Container queries (@container) let you style elements based on the size of their parent container rather than the viewport. This enables truly reusable components that adapt to wherever they're placed, unlike media queries which only respond to the browser window size.
Previous
Cascade Layers (@layer)
Next
CSS Custom Properties (Variables)
PrevNext