Frontend

CSS Container Queries: The Future of Responsive Design

Mayur Dabhi
Mayur Dabhi
May 7, 2026
13 min read

You build a card component, test it at a few viewport widths with @media (max-width: 768px), and ship it. Then someone drops the same card into a 250px sidebar on a 1440px screen. The media query still thinks it's on a desktop, so the card tries to render its wide layout and the image and text crush each other. The card has no idea how much room it actually has.

Container queries fix that by letting a component respond to the size of its parent container instead of the viewport. This post covers the syntax, the new cq* units, three patterns I actually use (cards, nav, tables), browser support, and the layout gotcha that trips up almost everyone the first time.

The problem they solve

In a design system, the same card should look different in a narrow sidebar and a wide main column. Before container queries you handled that with JavaScript, modifier classes like .card--compact, or a pile of media queries that guessed where the card would end up. Now the card can decide for itself.

@container vs @media: The Core Difference

Picture one product card used in three places on the same page: a full-width hero, a 3-column grid, and a narrow sidebar widget. With @media, all three copies react to the viewport, not to the space they've been given. So you write layout-specific overrides, and those break the next time someone rearranges the page.

@media (viewport-based) Viewport (1200px) Sidebar Card (200px wide) Broken! Main Content Card (viewport: 1200px) Looks OK Both cards react to same viewport width @container (component-based) Viewport (1200px) Sidebar Card reacts to 70px Adapts! Main Content Card reacts to 145px Adapts! container-type: inline-size container-type: inline-size

Media queries react to the viewport; container queries react to the parent container

Syntax and Setup

There are two parts. First, you declare an element as a containment context using the container-type property. Then, you write rules that apply based on that container's size using the @container at-rule.

Step 1: Declare a Container

1

Set container-type on the parent

Apply container-type: inline-size to the element whose width you want child components to respond to. Use size to respond to both width and height.

CSS: Declaring a container
/* The wrapper becomes a containment context */
.card-wrapper {
    container-type: inline-size;
    /* Optional: give it a name for targeted queries */
    container-name: card;

    /* Shorthand: name / type */
    container: card / inline-size;
}

Step 2: Write @container Rules

2

Write @container rules for child elements

Inside @container, select child elements and override their styles based on the container's dimensions, just like media queries but scoped to the container.

CSS: Responding to container size
/* Default styles: stacked layout */
.card {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
}

.card__image {
    width: 100%;
    aspect-ratio: 16/9;
    border-radius: 8px;
    object-fit: cover;
}

/* When the container is at least 400px wide: side-by-side layout */
@container (min-width: 400px) {
    .card {
        flex-direction: row;
        align-items: center;
    }

    .card__image {
        width: 160px;
        flex-shrink: 0;
        aspect-ratio: 1;
    }
}

/* When the container is at least 600px wide: larger text + extra info */
@container (min-width: 600px) {
    .card__title {
        font-size: 1.4rem;
    }

    .card__extra-info {
        display: block; /* Hidden by default */
    }
}

Named Containers for Precision

When you have nested containers or multiple containers on a page, named containers let you target the right ancestor. Without a name, @container matches the nearest container ancestor.

CSS: Named containers
.sidebar {
    container: sidebar / inline-size;
}

.main-content {
    container: main / inline-size;
}

/* Only triggers when the .sidebar container is narrow */
@container sidebar (max-width: 250px) {
    .widget {
        font-size: 0.85rem;
        padding: 8px;
    }
}

/* Only triggers when the .main-content container is wide */
@container main (min-width: 700px) {
    .article-card {
        display: grid;
        grid-template-columns: 200px 1fr;
    }
}
Gotcha: a container can't query itself

Only descendants of a container can respond to its size. If you put container-type on .card, your @container rules can style the card's children but not .card itself. That's deliberate: if an element could change its own size based on its own size, layout would loop. This is why you usually see an extra wrapper like .card-wrapper.

Gotcha: the container collapses to zero width

container-type: inline-size applies size containment on the inline axis, which means the element's width no longer depends on its content. In a normal block layout that's fine. But if the container is shrink-to-fit (a flex item without a set width, an inline-block, a float, an absolutely positioned element, or a grid track sized auto), it can collapse to 0px and your component just disappears. If a component vanishes the moment you add container-type, give the container an explicit width or flex: 1. The same thing happens on the block axis with container-type: size if the element has no explicit height.

Container Query Units

The spec also adds length units that are relative to the queried container, the same way vw and vh are relative to the viewport. If there's no container ancestor, they fall back to the small viewport units.

Unit Meaning Equivalent
cqw 1% of container's width Like vw but for container
cqh 1% of container's height Like vh but for container
cqi 1% of container's inline size Width in horizontal writing modes
cqb 1% of container's block size Height in horizontal writing modes
cqmin Smaller of cqi and cqb Like vmin
cqmax Larger of cqi and cqb Like vmax

Where I find them most useful is fluid type and spacing that scales with the component instead of the screen. Wrap them in clamp() or add a max size, otherwise text in a very wide container gets silly:

CSS: Container query units in practice
.widget-wrapper {
    container-type: inline-size;
}

.widget-title {
    /* Font size scales from ~14px (tiny container) to ~28px (wide container) */
    font-size: clamp(0.875rem, 4cqi, 1.75rem);
}

.widget-icon {
    /* Icon stays proportional to container */
    width: 10cqi;
    height: 10cqi;
    max-width: 48px;
    max-height: 48px;
}

.widget-padding {
    /* Padding scales with available space */
    padding: 2cqi 3cqi;
}

Real-World Use Cases

These are the three places where container queries have paid off most for me in component-heavy frontends.

1. The Adaptive Card Component

The textbook case: a card that stacks vertically in a narrow grid column and switches to a horizontal layout when it gets more room.

<!-- In a narrow sidebar -->
<aside class="sidebar">
    <div class="card-container">
        <article class="product-card">
            <img class="product-card__img" src="shoe.jpg" alt="Running Shoe">
            <div class="product-card__body">
                <h3>Air Runner Pro</h3>
                <p class="product-card__desc">Lightweight trail shoe for serious runners.</p>
                <span class="product-card__price">$129</span>
            </div>
        </article>
    </div>
</aside>

<!-- Exact same markup in main content -->
<main class="main-grid">
    <div class="card-container">
        <article class="product-card">
            <!-- Same structure -->
        </article>
    </div>
</main>
.card-container {
    container-type: inline-size;
}

/* Base: stacked, compact */
.product-card {
    display: flex;
    flex-direction: column;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    overflow: hidden;
}

.product-card__img {
    width: 100%;
    aspect-ratio: 4/3;
    object-fit: cover;
}

.product-card__body {
    padding: 12px;
}

.product-card__desc {
    display: none; /* Hidden when too narrow */
}

/* 280px+: show description */
@container (min-width: 280px) {
    .product-card__desc {
        display: block;
        font-size: 0.85rem;
        color: #718096;
    }
}

/* 380px+: go horizontal */
@container (min-width: 380px) {
    .product-card {
        flex-direction: row;
        align-items: stretch;
    }

    .product-card__img {
        width: 140px;
        aspect-ratio: 1;
        flex-shrink: 0;
    }

    .product-card__body {
        padding: 16px;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
}

/* 560px+: full-featured hero card */
@container (min-width: 560px) {
    .product-card__img {
        width: 220px;
    }

    .product-card__price {
        font-size: 1.4rem;
        font-weight: 700;
    }
}

The same .product-card markup ends up in four shapes depending on its container:

  • Under 280px container: Compact card with image, title and price
  • 280-379px container: Adds description text below the image
  • 380-559px container: Image and text side by side
  • 560px+ container: Roomier hero card with larger typography

No JavaScript and no .card--sidebar style modifier classes. Move the card somewhere else and it works out its layout on its own.

2. Navigation That Adapts to Its Container

Navigation is another good fit. In a full-width header the nav can lay its links out in a row; drop the same nav into a sidebar and it should become a compact vertical list.

CSS: Adaptive navigation
.nav-wrapper {
    container-type: inline-size;
}

/* Default: vertical stack */
.nav-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    list-style: none;
    padding: 0;
}

.nav-item a {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 6px;
    text-decoration: none;
    color: inherit;
}

/* 240px+: show icon + label */
@container (min-width: 240px) {
    .nav-item .nav-label {
        display: inline;
    }
}

/* 480px+: horizontal tabs */
@container (min-width: 480px) {
    .nav-list {
        flex-direction: row;
        gap: 8px;
    }

    .nav-item a {
        padding: 10px 16px;
    }
}

3. Data Tables That Reflow

A wide table squeezed into a narrow dashboard panel is painful to read. With a container query you can reflow each row into a small card once the panel is too narrow for all the columns. Each <td> needs a data-label attribute for this to work, since the header row gets hidden.

CSS: Responsive table with container queries
.table-wrapper {
    container-type: inline-size;
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid #e2e8f0;
}

/* Below 500px: reflow to card layout */
@container (max-width: 500px) {
    table, thead, tbody, th, td, tr {
        display: block;
    }

    thead tr {
        /* Hide column headers; labels come from data-label */
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    tr {
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        margin-bottom: 12px;
        padding: 8px;
    }

    td {
        border: none;
        padding: 6px 8px;
        display: flex;
        justify-content: space-between;
    }

    td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #718096;
    }
}

Browser Support and Progressive Enhancement

Size container queries shipped in Chrome and Safari in late 2022 and in Firefox in early 2023, so they've been Baseline for a while now. Unless you still support very old browsers, you can use them in production.

Browser @container Support Since CQ Units Support Since Global Usage
Chrome / Edge 105 (Aug 2022) 105 (Aug 2022) ~72%
Firefox 110 (Feb 2023) 110 (Feb 2023) ~4%
Safari 16 (Sep 2022) 16 (Sep 2022) ~19%
IE 11 Not supported Not supported <0.5%

Progressive Enhancement Pattern

Write the default styles first, with no container queries, then layer the container rules on top. Older browsers just get the default (usually stacked) layout, which is a fine fallback. The last snippet below is JavaScript, not CSS, and belongs in a script file.

CSS: Progressive enhancement approach
/* Base styles work everywhere */
.card {
    display: flex;
    flex-direction: column;
    padding: 16px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

/* Optional: wrap in @supports for extra safety */
@supports (container-type: inline-size) {
    .card-wrapper {
        container-type: inline-size;
    }

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

/* Can also use feature detection in JS */
if (CSS.supports('container-type', 'inline-size')) {
    document.body.classList.add('supports-cq');
}
About polyfills

There is a container-query-polyfill package from Google Chrome Labs, but it runs in JavaScript and adds overhead to every page. I'd skip it. Progressive enhancement is simpler, and the fallback layout is almost always acceptable.

Container Queries vs Other Responsive Techniques

@media queries + Universal support + Simple to write - Viewport-only - Context-unaware - Breaks in reuse @container queries + Context-aware + Truly reusable + No JS required ± Modern browsers ± Wrapper required JS-based (ResizeObserver) + Maximum flexibility - JS overhead - Layout thrashing risk - More complex code - Not declarative Recommended

Comparing responsive design techniques for component-level adaptation

I use media queries for page-level decisions: header height, whether the sidebar shows, how many columns the main grid has. I use container queries for how a component renders inside the space it was given. Most real projects need both. ResizeObserver is still the right tool when you need the size in JavaScript, for example to decide how many chart ticks to draw.

Advanced Patterns and Tips

Style Queries (Still Rolling Out)

Style queries let you query a container's custom property values instead of its dimensions, which opens up theming patterns where children react to a variable on the parent. Chromium and Safari support them for custom properties; Firefox support was still pending last I checked, so treat this as an enhancement. One correction to a pattern you'll see in a lot of examples, including the one below: you don't need container-type: style. Every element is already a style container, and style isn't a valid container-type value, so browsers ignore that line.

CSS: Style queries (newer feature)
/* Style query: respond to a CSS custom property on the container */
.card-wrapper {
    container-type: style;
    --card-variant: featured;
}

/* Children can query parent's custom property value */
@container style(--card-variant: featured) {
    .card {
        background: linear-gradient(135deg, #7c3aed, #4f46e5);
        color: white;
        transform: scale(1.02);
        box-shadow: 0 10px 40px rgba(124, 58, 237, 0.4);
    }
}

Combining with CSS Grid and Flexbox

This is the combination I reach for most. Let CSS Grid's auto-fill and minmax() decide how many columns fit, then let container queries decide how the content inside each cell renders. The grid tracks here have a definite size from minmax(), so the cells don't hit the zero-width collapse described earlier:

CSS: Container queries + CSS Grid
/* Grid adapts to viewport/parent */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
}

/* Each cell is a container */
.product-grid > * {
    container-type: inline-size;
}

/* Card content adapts to cell width */
@container (min-width: 300px) {
    .product-card {
        /* wider card layout */
    }
}

/* For large containers (e.g. featured product slot) */
@container (min-width: 500px) {
    .product-card {
        /* hero layout */
    }
}

Container Query Quick-Reference Cheat Sheet

Feature Syntax Notes
Declare container (inline) container-type: inline-size Responds to width only
Declare container (both axes) container-type: size Responds to width & height
Name a container container-name: sidebar Enables targeted queries
Shorthand container: sidebar / inline-size name / type
Min-width query @container (min-width: 400px) Mobile-first approach
Max-width query @container (max-width: 400px) Desktop-first approach
Named query @container sidebar (min-width: 200px) Targets a specific container
CQ unit font-size: 4cqi 4% of container's inline size
Feature detection @supports (container-type: inline-size) Safe progressive enhancement

When I'd use container queries (and when I wouldn't)

I'd use them for any component that gets placed in more than one layout: cards, widgets, nav blocks, table panels, anything that lives in a design system or a CMS where editors move blocks around. That's where viewport breakpoints lie to you the most.

Where I'd hold back

  • Page-level layout: Whether the sidebar shows or how many columns the page has is a viewport question. A media query is simpler and clearer there.
  • Wrapping everything in containers: Each container adds an extra wrapper and a containment boundary. Add one where a component actually needs it, not on every div "just in case".
  • Shrink-to-fit parents: If the container sits in a flex row or an auto grid track, check that it doesn't collapse to zero width before you ship.
  • Unnamed nested containers: Once containers nest, an unnamed @container query matches the nearest one, which may not be the one you meant. Name them.
  • Style queries in critical UI: Fine as an enhancement, but don't rely on them for anything that has to look right in every browser yet.

If you want to try them on an existing project, pick one component that already has a --compact or --sidebar modifier class and see if a container query can replace it. That's usually the quickest win, and it tells you fast whether your layout has any of the collapsing containers mentioned above.

CSS Container Queries Responsive Design Frontend @container Web Standards
Mayur Dabhi

Mayur Dabhi

Full Stack Developer with 5+ years of experience building scalable web applications with Laravel, React, and Node.js.