Tailwind Container Queries: The Complete Guide

October 13, 2026  |  11 min read  |  Tailwind CSS

By Syed · Frontend Engineer & Creator of UITweak

Media queries answer one question: how wide is the viewport? But reusable components need a different question: how wide is my parent? A card dropped into a narrow sidebar should look different from the same card stretched across a main column, regardless of screen size. Container queries solve exactly this, and Tailwind ships them built in.

Shortcut: prototype responsive card layouts visually in our free Tailwind Grid Builder, then make the cards container-aware with the technique below.

1. Mark a Container with @container

Any element becomes a queryable container with the @container class. Children can then use container variants like @md: that respond to the container width instead of the viewport:

<div class="@container">
  <article class="grid gap-4 @md:grid-cols-2 @lg:grid-cols-3">
    <!-- 1 column when the container is narrow,
         2 at md container width, 3 at lg -->
  </article>
</div>

The syntax mirrors responsive prefixes you already know: @sm:, @md:, @lg:, @xl:, @2xl:, plus arbitrary values like @[520px]:grid-cols-2. The only difference is what they measure.

2. Named Containers for Nested Layouts

When containers nest, name them so each child queries the right ancestor:

<div class="@container/main">
  <aside class="@container/sidebar">
    <!-- Queries the sidebar container, not the outer one -->
    <div class="@md/sidebar:flex-row flex-col">...</div>
  </aside>
  <!-- Queries the main container -->
  <div class="@lg/main:grid-cols-2">...</div>
</div>

Without names, a child queries its nearest container ancestor, which is usually what you want. Reach for names when layouts nest two or more levels deep.

3. Recipe: A Card That Adapts Anywhere

The canonical use case. One card component, two completely different layouts depending on where it is placed:

<div class="@container">
  <article class="flex flex-col gap-3 p-4 @md:flex-row @md:items-center @md:gap-6 @md:p-6">
    <img class="size-16 rounded-xl @md:size-24" src="..." alt="" />
    <div>
      <h3 class="font-bold">Component title</h3>
      <p class="text-sm text-slate-500 @md:text-base">
        Stacked and compact in a sidebar, roomy and horizontal
        in a wide column. Same markup, no props, no JS.
      </p>
    </div>
  </article>
</div>

Drop this card in a 280px sidebar and it stacks. Drop the identical markup in an 800px content area and it goes horizontal. That is reusability media queries could never give you.

4. Container Queries vs Media Queries

  • Use container queries for components: cards, widgets, nav items, anything reused across different layout slots.
  • Use media queries for page-level decisions: overall layout, showing or hiding the sidebar itself, typography scale.
  • They compose: a page can switch layout with md: while each card inside adapts with @md:. Neither replaces the other.

5. Custom Container Breakpoints

The default container breakpoints match the viewport scale, but containers are often narrower than screens. In Tailwind v4, define container-specific breakpoints in your theme:

@theme {
  --breakpoint-card-sm: 22rem;
  --breakpoint-card-lg: 34rem;
}

<!-- Usage -->
<div class="@container">
  <div class="@card-sm:grid-cols-2 @card-lg:grid-cols-3">...</div>
</div>

6. Container Query Units: cqw and cqh

Beyond breakpoints, containers unlock relative units. 1cqw is 1 percent of the container width, 1cqh of its height. Typography that scales with its card instead of the viewport:

<div class="@container">
  <h3 class="text-[6cqw] font-bold">
    <!-- Grows as the card grows, shrinks as it shrinks -->
    Scales with the container
  </h3>
</div>

Cap it with clamp() so text never gets absurd: text-[clamp(1rem,6cqw,2.5rem)]. This is fluid type done right, bound to the component instead of the screen.

7. Gotchas Before You Ship

A container cannot query itself

The most common mistake: putting @container and @md:... on the same element. Container queries style descendants based on an ancestor. The queryable element and the responding element must be different nodes.

container-type: inline-size collapses height

Tailwind's @container sets container-type: inline-size, which makes the element's height no longer derive from its content in some layouts. If a container mysteriously collapses, that is why. Give it an explicit height or min-height.

Nesting depth costs performance

Each container establishes a new query scope the browser must track. Two or three levels is fine; ten nested @container divs on every card will show up in your performance profiles.

8. Browser Support and Fallback

Container queries are supported in all modern browsers, so no polyfill is needed for current audiences. For graceful degradation, write the narrow layout as the base and layer container variants on top: older browsers simply render the compact stacked version, which is always a safe default.

9. Where This Shines

  • Design systems: one card, used in dashboards, sidebars, and modals, adapting itself each time.
  • Bento grids: tiles that reflow their internals based on tile size, not screen size. Pair with our Tailwind v4 @theme guide for custom container breakpoints.
  • Embedded widgets: components dropped into third-party pages where you cannot control the viewport.

Build the outer layout in our free Tailwind Grid Builder, then make every card inside it container-aware with @container.

Height Queries Need container-type: size, and It Has a Cost

Tailwind's @container sets container-type: inline-size, which enables width queries only. Querying height requires container-type: size, and that comes with size containment: the element's size may no longer depend on its contents. In practice the height collapses unless you set it explicitly.

<div class="@container [container-type:size] h-96">
  <!-- descendants can now query this box's height -->
  <div class="tile-body">...</div>
</div>
/* Tailwind has no height-query variants, so write the raw rule */
@container (min-height: 24rem) {
  .tile-body { grid-template-columns: 1fr 1fr; }
}

I avoid height queries unless the layout has fixed-height regions, like dashboard tiles or app shells with known header heights. Content-driven height plus height queries is a loop the browser resolves by collapsing, which is never what you wanted. Width queries cover the vast majority of component cases; reach for height only when the design genuinely depends on vertical space.

Container Units for Spacing, Not Just Type

Container query units work anywhere a length works, not just font-size. Padding, gaps, and radii in cqw keep a component's visual density constant as it resizes:

<div class="@container">
  <article class="p-[5cqw] rounded-[2cqw]">
    <div class="grid gap-[3cqw] @md:grid-cols-2">
      <!-- padding and gaps scale with the card -->
    </div>
  </article>
</div>

A card with p-[5cqw] has proportionally identical padding at 280px and 800px wide, which breakpoint-based padding can only approximate with jumps. Cap the extremes with clamp() so padding never vanishes or explodes: p-[clamp(1rem,5cqw,3rem)]. I use cqw spacing on bento tiles and dashboard cards, anywhere the same component renders at wildly different sizes. It replaces three breakpoints with one declaration.

Debugging Containers in DevTools

Two tools make container queries debuggable instead of mysterious. First, Chrome's Elements panel marks query containers with a container badge in the DOM tree. Click it and DevTools shows the container's name, type, and which queries currently match. When an @md: variant is not applying, the badge tells you in one glance whether the ancestor is actually a container and how wide it really is.

Second, gate enhancements behind @supports so the fallback is a deliberate choice:

.card { /* narrow stacked layout: the safe default */ }

@supports (container-type: inline-size) {
  @container (min-width: 30rem) {
    .card { /* wide layout, only where queries work */ }
  }
}

Older browsers ignore the whole block and keep the stacked layout, which is exactly the graceful degradation the main guide recommends, made explicit. Between the badge and the guard, container query bugs stop being guesswork.

Know What Container Queries Cannot Do

Container queries answer exactly one question: how big is my box. They cannot respond to content (a long title versus a short one), to state (this item is selected), or to siblings. When the trigger is content rather than size, :has() is usually the right tool: .card:has(img) restyles cards that contain an image, with no container needed.

I also avoid wrapping leaf components in containers by default. Each @container adds a query scope the browser tracks, and a page of fifty container-wrapped badges pays a real cost for zero benefit. Put containers at layout boundaries, sidebars, grids, dialogs, and let the leaves inherit the responsiveness. Media queries still own the page, container queries own the component, and :has() owns the content.

FAQ

Do I need the container-queries plugin in Tailwind v4?

No. Container queries are built into Tailwind v3.2+ and v4 core. The plugin only matters for older v3 versions. If @container does nothing, check your Tailwind version first.

Can I combine @container with dark mode?

Yes, variants compose freely: dark:@md:grid-cols-2 works exactly as you would expect. Order does not matter much, but keep it readable and consistent across your codebase.

Why is my @md: variant not applying?

Three usual suspects: the ancestor is missing @container, the @md: class is on the container element itself (it must be on a descendant), or the container is narrower than you think. Add a temporary outline to see its real bounds.

Are container queries bad for performance?

Used normally, no. Browsers heavily optimize them. They only get expensive with deep nesting or when the container size changes every frame (for example, animating the container width itself).

Share this guide