πŸ“¦ @container β€” The Future of Component-Level Responsive Design

🌐 What is @container?

@container is a modern CSS at-rule that enablescontainer queries β€” meaning elements can adapt based on the size of their parent container instead of the entire viewport! This solves one of the biggest limitations in responsive design.

>>β€œ@container makes components truly responsive β€” independent of the page layout.”

Note

πŸ“Œ Browser Support: Excellent across modern browsers (Chrome, Edge, Safari, Firefox).

🎯 Why Do We Need @container?

  • Responsive components that adapt to their parent width
  • No more relying only on viewport-based media queries
  • Reusable, modular UI components
  • Better control inside grids, cards, sidebars, etc.

πŸ“Œ How @container Works

Two steps:

  1. Enable containment on a parent container.
  2. Write container queries that react to the container’s size, not the viewport.

🧱 Step 1 β€” Enable Containment

Apply container-type to a parent element:

Enable container

.card {
  container-type: inline-size; /* reacts to width only */
}

inline-size β†’ listens to width
size β†’ listens to width + height

Note

βœ” Most components use inline-size (horizontal responsiveness).

🧱 Step 2 β€” Write a Container Query

Basic Container Query

@container (min-width: 400px) {
  .title {
    font-size: 2rem;
  }
}

The `.title` becomes larger only when its parent container becomes 400px wide β€” not the viewport.

πŸ“ Visual Example

Media content

πŸ”₯ Full Component Example

Card With Container Query

.card {
  padding: 20px;
  border: 1px solid #ddd;
  container-type: inline-size;
}

.card-content {
  display: flex;
  flex-direction: column;
}

@container (min-width: 500px) {
  .card-content {
    flex-direction: row;
    gap: 20px;
  }
}

When the card becomes wide enough, the layout switches to side-by-side view (even if the viewport is still small!).

🎨 Responsive Typography Using @container

Container-based typography

.section {
  container-type: inline-size;
}

@container (min-width: 600px) {
  h1 {
    font-size: 3rem;
  }
}

@container (max-width: 599px) {
  h1 {
    font-size: 2rem;
  }
}

The heading adjusts based on section width instead of screen size.

πŸ“¦ Container Names

Naming Containers

.wrapper {
  container-type: inline-size;
  container-name: layout;
}

@container layout (min-width: 800px) {
  .sidebar {
    display: block;
  }
}

Naming allows writing container queries that target specific container types.

🧠 Nested Containers

Container queries work independently for each container β€” meaning **each component is fully responsive inside its own space**.

Media content

πŸ“± Example β€” Buttons Growing With Container

Button Container Query

.button-group {
  container-type: inline-size;
}

@container (min-width: 350px) {
  button {
    padding: 1rem 2rem;
    font-size: 1.2rem;
  }
}

Buttons get larger only when their parent group expands β€” perfect for toolbars or cards.

πŸ§ͺ Range Queries + @container (Level 4 Syntax)

Range Query with Container

@container (400px <= width <= 800px) {
  .grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

Works exactly like viewport range queries, but applied to container width.

⚠️ Common Mistakes

  • Forgetting to set `container-type` β†’ queries won’t work
  • Applying @container to elements that never shrink or grow
  • Confusing @container with @media (one is component-based, one is viewport-based)
  • Using height-based queries without `container-type: size`

Note

πŸ’‘ @media = viewport responsive
@container = component responsive
Use BOTH for best results!

πŸ”₯ Summary

@container is a groundbreaking feature that makes CSS truly modular and adaptive. It enables components to respond to their available space instead of the entire viewport, making reusable UI systems far more reliable and flexible.

>>β€œContainer queries unlock true component-driven design β€” the future of responsive CSS.”

Learn more β†’MDN Docs πŸ“˜