Daily Dev Post logo
Daily Dev Post
Published on, Time to read
🕒 8 min read

What Are CSS Container Queries and Why Are They Replacing Media Queries?

What Are CSS Container Queries and Why Are They Replacing Media Queries?

If you’re still building responsive components with only media queries, you’re solving the wrong problem.

Media queries respond to the screen.
CSS container queries respond to context.

And in a component-driven world, context wins.

CSS container queries let a component respond to the size of its parent container instead of the viewport. Media queries react to screen width. Container queries react to layout context. That shift makes responsive components predictable, reusable and aligned with how we build modern UI systems.

If you build design systems or ship React components across different layouts, CSS container queries solve problems media queries never could.

I have been building responsive interfaces for over a decade. Media queries carried us for years. But they were built for pages. Not components. And that distinction matters more today than it did five years ago.

Let’s break this down properly.

The Problem: Media Queries Were Built for Pages, Not Components

Media queries work. I am not here to say they are broken.

But they assume one thing. The viewport is the source of truth.

That assumption starts to fall apart when your UI is component driven.

The Limits of Media Queries in Component-Based UI

Here is a common pattern:

@media (min-width: 768px) {
  .card {
    display: flex;
    gap: 1rem;
  }
}

Looks fine. Works fine.

Until that same .card lives inside:

  • A narrow sidebar
  • A CMS layout block
  • A grid cell
  • A resizable dashboard panel

Now the viewport might be 1440px wide. But the container holding the card is 320px.

The card switches to horizontal layout because the screen is wide. But it does not have space.

This is the core flaw.

Media queries are page-level. Our components are context-level.

Why Responsive Components Break with Traditional Media Queries

In real projects, this leads to:

  • Overriding media queries in edge cases
  • Adding modifier classes just to fight layout context
  • Creating breakpoint chaos across design systems
  • Writing increasingly defensive CSS

I have seen component libraries where breakpoints are duplicated across files just to patch layout mismatches.

That is technical debt created by architecture, not syntax.

When people compare container queries vs media queries, they usually focus on syntax. That misses the point.

This is about mental models.

The Solution: CSS Container Queries Change the Mental Model

CSS container queries flip the dependency.

Instead of asking:

How wide is the screen?

We ask:

How wide is the component’s container?

That single change makes responsive components behave the way we always wanted them to.

How CSS Container Queries Work

At a high level, you define a container. Then you query against it.

Step 1: Declare a Container

.card-wrapper {
  container-type: inline-size;
}

Now .card-wrapper becomes a queryable container.

Step 2: Use the @container Rule

@container (min-width: 400px) {
  .card {
    display: flex;
    gap: 1rem;
  }
}

That is it.

The .card now responds to the width of .card-wrapper. Not the viewport.

This is the core of how to use container queries in CSS. It is built around the @container rule and container-type: inline-size.

No hacks. No JS. No ResizeObserver.

Just CSS.

Container Queries vs Media Queries: Architectural Impact

Media queries couple components to global breakpoints.

Container queries decouple components from page structure.

That matters in:

  • Design systems
  • Micro frontends
  • CMS-driven layouts
  • Dashboard applications
  • Reusable component libraries

When I switched a card system in one project to CSS container queries, something interesting happened. The component stopped caring about where it lived.

That is powerful.

It also simplifies reasoning. A component defines its own responsive behavior. You do not have to scan global styles to understand it.

This is why I consider CSS container queries the next evolution of responsive CSS. Not a replacement. An evolution.

Implementation: Building Responsive Components with CSS Container Queries

Let’s build something real.

We will create a responsive card that adapts between stacked and horizontal layout based on container width.

Step 1: Define a Query Container

.dashboard-panel {
  container-type: inline-size;
  padding: 1rem;
}

You can also name containers:

.dashboard-panel {
  container-type: inline-size;
  container-name: panel;
}

Naming helps when multiple containers are nested.

Step 2: Write Responsive Rules with @container

.card {
  display: grid;
  gap: 1rem;
}

@container panel (min-width: 500px) {
  .card {
    grid-template-columns: 200px 1fr;
    align-items: center;
  }
}

Now this card adapts based on the panel it lives in.

If the panel is narrow, the card stacks.

If the panel grows, the card becomes two columns.

We did not touch the viewport.

Converting Media Queries to Container Queries

Most teams hesitate here.

They ask:

How do I convert media queries to container queries?

You do not convert everything.

Start with components reused in multiple layout contexts.

Old Approach

@media (min-width: 1024px) {
  .profile-card {
    display: flex;
  }
}

New Approach

.profile-card-wrapper {
  container-type: inline-size;
}

@container (min-width: 600px) {
  .profile-card {
    display: flex;
  }
}

Notice something important.

The breakpoint changed.

Container queries often require smaller thresholds because containers are smaller than viewports.

Migration Strategy

  • Keep global layout media queries
  • Move component-level responsiveness to container queries
  • Remove redundant overrides over time

Hybrid works well.

Comparison Table: Container Queries vs Media Queries

FeatureCSS Container QueriesMedia Queries
Triggered ByParent container sizeViewport size
Responsive ComponentsNative and scopedCoupled to global layout
Design System FriendlyYesPartially
ReusabilityHighContext dependent
Architecture FitComponent drivenPage driven
Browser SupportModern browsersUniversal

In short:

Media queries solve layout responsiveness.
CSS container queries solve component responsiveness.

Pitfalls and When Not to Use Container Queries

Container queries are not magic.

There are tradeoffs.

Performance Considerations

Each container creates a new containment context.

Deep nesting can increase layout calculations.

In practice, I have not seen meaningful performance regressions in real apps. But you should:

  • Avoid unnecessary containers
  • Scope them to actual responsive components
  • Profile complex dashboards

Overengineering Small Projects

If you are building:

  • A simple marketing site
  • A static landing page
  • A one-off layout

Media queries are fine.

Do not introduce container queries just because they are new.

Browser Support and Fallback Strategy

If you support legacy environments:

@supports (container-type: inline-size) {
  .card-wrapper {
    container-type: inline-size;
  }
}

Older browsers will ignore container queries and fall back to default stacked layout.

When Should You Use Container Queries Instead of Media Queries?

Use Container Queries When:

  • You build reusable components
  • You ship a design system
  • Your UI lives in dynamic containers
  • Layout context changes frequently
  • You want predictable component behavior

Use Media Queries When:

  • You adjust global page layout
  • You manage major grid changes at viewport level
  • You target legacy browsers heavily

I treat media queries as macro layout tools.

I treat CSS container queries as micro layout tools.

Different scopes. Different responsibilities.

FAQ

What is the difference between container queries and media queries?

Media queries respond to the viewport size. Container queries respond to the size of a parent container. Media queries are page oriented. Container queries are component oriented.

How do I fix layout issues using CSS container queries?

Define a query container with container-type: inline-size. Move viewport-based breakpoints into @container rules scoped to that container. Adjust thresholds to reflect container widths.

Are CSS container queries better than media queries?

They are better for component driven architecture. They are not a replacement for global layout logic. In most real projects, you will use both.

The Bigger Shift

The real shift is architectural.

Frontend used to be page centric.

Now it is component centric.

React, Vue, design systems, headless CMS, micro frontends all push us toward modular UI thinking.

CSS container queries match that direction.

They reduce coupling.

They make components self aware of their layout context.

They remove the need for defensive overrides tied to viewport assumptions.

That is why CSS container queries are the new media queries.

Not because media queries are obsolete.

But because the center of gravity moved.

From page to component.

If you are still writing responsive logic around the viewport, you are thinking at the wrong level for modern systems.

Adopt container queries where they make sense. Keep media queries where they belong.

Design your CSS the same way you design your JavaScript.

Modular. Scoped. Intentional.

That is how responsive design should feel in 2026.

Did you like the article? Support me on Ko-Fi!

Pradip Jarhad

Pradip Jarhad

Creator @ DailyDevPost

I’m Pradip. Software Developer and the voice behind DailyDevPost. I translate daily development struggles into actionable lessons on React, JavaScript, and deep dive debugging. Built for the craftsman developer who values real world logic over theory. Stop building software and start engineering it.