🍋
Menu
Comparison Beginner 1 min read 159 words

CSS Container Queries vs Media Queries

Container queries let components respond to their container's size instead of the viewport. Learn when to use each approach for responsive design.

Key Takeaways

  • Media queries respond to the browser viewport width.
  • Container queries respond to the nearest containment context — the parent element's width.
  • Container queries are supported in all modern browsers (Chrome 105+, Firefox 110+, Safari 16+).
  • Media queries**: Page-level layout, navigation breakpoints, font scaling.
  • Container queries require a containment context on the parent element.

Media Queries: Viewport-Based

Media queries respond to the browser viewport width. They work well for page-level layout changes but fail for reusable components that appear in containers of different widths on the same page.

Container Queries: Component-Based

Container queries respond to the nearest containment context — the parent element's width. A card component can show a horizontal layout in a wide sidebar and a vertical layout in a narrow sidebar, all on the same viewport.

Browser Support

Container queries are supported in all modern browsers (Chrome 105+, Firefox 110+, Safari 16+). For older browsers, media queries remain the reliable fallback.

When to Use Each

  • Media queries: Page-level layout, navigation breakpoints, font scaling.
  • Container queries: Reusable components, card layouts, widget sizing.

Implementation

Container queries require a containment context on the parent element. Use container-type: inline-size to create a size container, then query it with @container (min-width: 400px) { ... } inside child rules.

Verwandte Tools

Verwandte Formate

Verwandte Anleitungen