Course contentsShow
← Web Development
Lesson 431 of 2,9827. Responsive DesignFree lesson

Container Queries vs Media Queries

Learning when to use container queries for component-level responsiveness versus viewport-based media queries.

Container Queries vs Media Queries

You'll learn when to use container queries for component-level responsiveness versus viewport-based media queries.

The Core Difference

Media queries respond to the viewport (the browser window), while container queries respond to the size of a parent container. Think of media queries as checking the size of the entire room, whereas container queries check the size of a specific shelf.

When to Use Media Queries

Use media queries when your design needs to adapt based on:

  • Overall page layout changes (sidebar visibility, column counts)
  • Global typography adjustments
  • Device-specific features (orientation, touch vs mouse)
  • Major layout shifts affecting the entire page
/* Layout changes based on screen width */
@media (min-width: 768px) {
  .page-layout {
    display: grid;
    grid-template-columns: 250px 1fr;
  }
}

When to Use Container Queries

Use container queries when a reusable component needs to adapt based on where it's placed:

  • Card components that appear in narrow sidebars or wide main areas
  • Navigation that might live in different-sized containers
  • Widgets that need to work in multiple contexts
@container (min-width: 400px) {
  .card {
    display: grid;
    grid-template-columns: 200px 1fr;
  }
}

Why This Matters

Container queries make components truly modular. A card doesn't need to "know" about the viewport—it just adapts to its container. This is perfect for design systems where the same component appears in various layout contexts.

Key Takeaway: Use media queries for page-level layout decisions based on viewport size; use container queries for component-level responsiveness based on container size.