Responsive web design has traditionally relied on media queries. Developers define breakpoints based on the viewport width, then adjust layouts for phones, tablets, and larger screens.

This approach remains useful for page-level design, but it has an important limitation: a component does not necessarily know how much space it has available.

A card displayed inside a narrow sidebar may require a compact layout even when the browser window is wide. The same card placed in a full-width content area may have enough room for a horizontal layout.

CSS container queries address this problem by allowing components to respond to the dimensions of their containing elements rather than relying exclusively on viewport dimensions.

The Limitation of Viewport Breakpoints

Consider a reusable article card used in three locations: the homepage, a category archive, and a sidebar.

A traditional media query might change the card’s layout when the viewport exceeds 900 pixels.

@media (min-width: 900px) {
    .article-card {
        display: grid;
        grid-template-columns: 180px 1fr;
    }
}

Unfortunately, the sidebar may remain narrow even on a 1600-pixel-wide screen.

The media query cannot distinguish between the card’s actual available width and the width of the entire browser window.

Developers often work around this limitation by introducing location-specific classes or additional CSS selectors.

Over time, those exceptions can make reusable components difficult to maintain.

Introducing Container Queries

Container queries allow CSS rules to respond to conditions associated with an ancestor element.

For size-based queries, developers first establish a query container:

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

The inline-size value enables queries based on the container’s inline dimension, which normally corresponds to width in horizontal writing modes.

We can then change descendant styles when the container becomes sufficiently wide:

@container (min-width: 500px) {
    .article-card {
        display: grid;
        grid-template-columns: 180px 1fr;
    }
}

The layout now responds to the component’s available space rather than the viewport.

A Complete Article Card Example

Let’s create a card that displays vertically in narrow containers and horizontally in wider ones.

The HTML structure is deliberately simple:

<div class="card-container">
    <article class="article-card">
        <img
            src="article-image.jpg"
            alt=""
            class="article-card__image"
        >

        <div class="article-card__content">
            <h2>Understanding Container Queries</h2>
            <p>
                Build components that adapt
                to their available space.
            </p>
        </div>
    </article>
</div>

The image filename is illustrative and should be replaced with a real local asset when testing the example.

Next, define the base styles and the container query:

.card-container {
    container-type: inline-size;
    container-name: article-card;
}

.article-card {
    display: grid;
    gap: 1rem;
    padding: 1rem;
    border: 1px solid #ddd;
    border-radius: 12px;
}

.article-card__image {
    display: block;
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
}

.article-card__content h2 {
    margin-top: 0;
}

@container article-card (min-width: 500px) {
    .article-card {
        grid-template-columns: 180px minmax(0, 1fr);
        align-items: start;
    }

    .article-card__image {
        aspect-ratio: 4 / 3;
    }
}

The card now has two layouts without requiring separate sidebar and homepage variants.

When the containing element is narrow, the image appears above the text. When the container reaches 500 pixels, the card switches to a horizontal arrangement.

Why Container Naming Matters

Container queries normally use an eligible ancestor container. In complex layouts, multiple ancestors may establish containment contexts.

Using container-name allows developers to target a specific containment context.

This becomes particularly useful in design systems containing nested components, where a card may appear inside a grid, panel, modal, or another reusable component.

Named containers make the relationship between a component and its responsive rules more explicit.

Container Query Units

CSS also provides container-relative length units.

For example, cqi represents one percent of a query container’s inline size.

.article-card__content h2 {
    font-size: clamp(
        1.1rem,
        4cqi,
        1.8rem
    );
}

This allows typography to respond to component dimensions, although developers should use minimum and maximum limits to maintain readability.

Container units are useful, but they should not replace thoughtful typography and accessibility testing.

Container Queries and Media Queries Are Complementary

Container queries do not make media queries obsolete.

Media queries remain appropriate for viewport-level concerns, including global navigation, page composition, and user preferences such as reduced motion.

Container queries are particularly useful for reusable components whose layout should depend on the space provided by their parent.

A practical architecture often uses media queries for the page and container queries for components.

Important Limitations

Container queries introduce containment rules that affect layout calculations.

For example, inline-size containment means the container’s inline dimension must be determined without relying on the intrinsic inline size of its descendants.

Developers should therefore apply containment deliberately rather than adding container-type to every element.

Another important distinction is that container queries style descendants of a container. They do not directly style the query container itself based on its own size.

For older browser environments, teams should verify support requirements and provide an acceptable default layout.

Conclusion

CSS container queries provide a more component-oriented approach to responsive design.

Instead of assuming that viewport dimensions describe the space available to every element, components can respond to their actual layout context.

For websites with reusable cards, dashboards, sidebars, and modular content blocks, this can significantly reduce location-specific styling and improve maintainability.

The most useful question is no longer simply how wide the screen is. It is how much space the component actually has.

LEAVE A REPLY

Please enter your comment!
Please enter your name here