Skip to main content

Container Queries

Container queries let you style descendants based on the size of a parent container rather than the viewport. This makes components truly context-agnostic β€” they adapt to the space they're given, not to the overall screen.

Browser support: Widely available Baseline since August 2025 (Chrome 105+, Edge 105+, Firefox 110+, Safari 16+). Became Newly available in February 2023 when Firefox 110 shipped support last.

Use cases​

  • Reusable card/widget components that adapt to their layout context
  • Sidebar vs. main content layouts where the same component renders differently
  • Design systems and component libraries that must work in varying column widths
  • Dashboard widgets and grid items

How it works​

You define a containment context on a parent element, then query that container's dimensions from its descendants using the @container at-rule.

Key properties:

  • container-type β€” declares the element as a query container (inline-size or size)
  • container-name β€” gives the container an optional name for targeted queries
  • container β€” shorthand combining name and type (e.g. container: sidebar / inline-size)
.card-wrapper {
container-type: inline-size;
container-name: card;
}

@container card (min-width: 400px) {
.card {
flex-direction: row;
gap: 1.5rem;
}
}
tip

Use named containers (container-name) in complex layouts to avoid ambiguity when multiple ancestor containers are defined.

Container query length units​

UnitRelative toDescription
cqwContainer width1% of container's width
cqhContainer height1% of container's height
cqiContainer inline size1% of container's inline axis
cqbContainer block size1% of container's block axis
cqminSmaller of cqi/cqb1% of the smaller dimension
cqmaxLarger of cqi/cqb1% of the larger dimension

These units are useful for fluid sizing that scales with the container:

.card-title {
font-size: clamp(1rem, 3cqi, 1.75rem);
}

Comparison with media queries​

FeatureMedia QueriesContainer Queries
Responds toViewport dimensionsNearest container ancestor's dimensions
ScopePage-levelComponent-level
ReusabilityComponent must know its page contextComponent is context-agnostic
Best forPage layout breakpointsAdaptive, reusable components

Using with Tailwind CSS​

Tailwind v4 (native)​

Tailwind v4 supports container queries out of the box β€” no plugin needed.

Mark a container with the @container class (or @container/name for named containers), then use size-variant prefixes on descendants:

PrefixMin-width
@3xs16rem
@xs20rem
@sm24rem
@md28rem
@lg32rem
@xl36rem
@2xl42rem
@4xl56rem
@7xl80rem

See the Tailwind container queries docs for the full list of breakpoints.

<div class="@container">
<div class="flex flex-col @md:flex-row gap-4">
<img src="/thumbnail.jpg" class="w-full @md:w-48" />
<div>
<h2 class="text-lg @lg:text-xl">Card title</h2>
<p>Card description…</p>
</div>
</div>
</div>

Tailwind v3 (plugin)​

Tailwind v3 requires the @tailwindcss/container-queries plugin.

npm install -D @tailwindcss/container-queries
tailwind.config.js
module.exports = {
plugins: [require("@tailwindcss/container-queries")],
};

Once installed the class API is the same as v4.

note

For more on our Tailwind CSS setup, see Tailwind CSS.

Gotchas​

  • Size containment required β€” The container must have a determinable inline-size. Place it inside a flex/grid context or give it an explicit width; otherwise it may collapse to zero.
  • Descendants only β€” You cannot query and style the container element itself; only its descendants respond to @container rules.
  • Style queries are limited β€” Container style queries (@container style(--theme: dark)) are supported in Chrome/Edge 111+, Safari 18+, and Firefox 151+. Not yet Widely available Baseline.
caution

Prefer inline-size β€” container-type: size requires containment on both axes, which breaks normal block-axis sizing (height depends on content). Use inline-size unless you have a fixed-height container.

When to use​

Use container queries when building reusable components that must adapt to available space regardless of viewport size β€” component libraries, dashboard widgets, CMS content regions. For page-level layout shifts that depend on the full viewport, media queries remain the right tool.

For a deeper dive with visual examples, see Ahmad Shadeed's container query guide.