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.
CSS Minifier
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.
Alat Terkait
Format Terkait
Panduan Terkait
CSS Units Explained: px, em, rem, vh, and When to Use Each
CSS offers over a dozen length units, each suited to different situations. Understanding the differences between absolute and relative units is essential for building responsive, accessible interfaces.
Flexbox vs CSS Grid: A Practical Comparison
Flexbox and CSS Grid are complementary layout systems, not competitors. This guide clarifies when to reach for each one and how to combine them for robust, responsive page layouts.
How to Create CSS Gradients: Linear, Radial, and Conic
CSS gradients create smooth color transitions without image files. Learn to build linear, radial, and conic gradients with precise control over color stops, direction, and shape.
Troubleshooting CSS Specificity Conflicts
When CSS rules unexpectedly override each other, specificity is usually the culprit. This guide explains how specificity is calculated and provides strategies for managing it in growing codebases.
CSS Custom Properties (Variables) Best Practices
CSS custom properties enable dynamic theming, design tokens, and maintainable style systems. Learn how to organize, scope, and use CSS variables effectively in production applications.