Skip to content
Putting technology to work.
Insights to guide decisions and action.

Search articles

Container queries not working — are you using them in place of screen width?

Table of contents · 5 items

An announcement card placed in the sidebar keeps its side-by-side layout in the narrow space and breaks. If you try to fix it with media queries, the branches keep multiplying, because at the same screen width the card displays correctly in the main column. Even when you apply container queries to this symptom, you can get stuck at "I wrote it, but nothing changes."

In September, Smashing Magazine pointed out that although browser support is in place, container queries are not used much and are easily misunderstood. The cause of misuse it cited was that they look almost identical to media queries.

Same shape, but measuring different things

@media (width > 40rem) and @container (width > 40rem) are written almost the same way. That visual similarity leads directly to misuse.

Media queries look at the viewport, that is, the screen itself. Container queries look at the dimensions of the ancestor element that wraps the element. The same card is wide when placed in the main column and narrow when placed in the sidebar. Container queries are the mechanism for switching according to that placement.

So replacing the places where you want to branch on screen width with container queries gains you nothing. The structure of the overall layout, such as whether to show a sidebar at all, remains the job of media queries, as before.

What you want to branch onWhat to use
Overall page structure (columns, whether there is a sidebar)Media queries
Component contents (card orientation, hiding elements)Container queries
Print, color scheme preference, reduced motionMedia queries (feature-specific conditions)

What to check, in order, when it doesn't work

If nothing happens after you write @container, the cause is almost always in one of a few predictable places.

  1. Is container-type set on the wrapping element? Unless you put inline-size on the parent side, the child's @container has nothing to refer to
  2. Is it referring to the ancestor you intended? If you don't specify one by name (container-name), the nearest ancestor with container-type is chosen. If there is another container in between, it looks at that one
  3. Is the element referring to itself? You can't change a container's own size with that container's query. It would create a loop: the width changes, the query result changes, and the width changes again

An editorial concept diagram showing that the nearest ancestor with container-type becomes the reference. Not an actual product screen or test result

The point to look at in the diagram is the second item. When elements with container-type are nested, the child refers not to the outer one but to the nearest inner one. When it doesn't branch at the width you intended, first check whether another container sits between the element and the ancestor. If you want a specific ancestor as the basis, give that ancestor a name with container-name and specify that name on the @container side.

The third item is an easy place to stumble when first learning this. Rather than changing the card's own width with @container, think of it as changing how the contents are arranged based on the box that wraps the card, and the design becomes more straightforward.

Where replacing is worth it

This is not about rewriting all the CSS on an existing site. Container queries pay off where the same component is placed in multiple locations.

In the CSS of sites that have been run for a long time, derived classes may have multiplied for each placement, such as card--sidebar card--narrow card--footer. Some of them were created only because "the width is different." Moving those variants to container queries brings the component back to one.

Conversely, there is no reason to use them for components placed in only one location, or components whose width doesn't change in the first place. The more containers you add, the more effort it takes to track which ancestor is being referenced.

We explain how to rebuild these as components in building UI that holds up wherever it is placed with container queries. For deciding which features you can rely on, see setting your support range with Baseline, and for moving processing you had been handling with JavaScript back into CSS, see shipping less JavaScript.

How to handle older browsers

In environments that don't support container queries, everything inside @container is ignored. You can handle this behavior through the order in which you write the CSS.

If you write the narrow appearance as regular CSS and put the changes for wider sizes inside @container, environments without support will display the narrow appearance. Instead of breaking, the layout simply doesn't change. If you make the wide version the default instead, unsupported environments will show the wide layout in narrow spaces, and it will break.

How to choose where to start

Start the refactoring with the component that has the most derived classes. Look at why each class was split off, one at a time, and count only those that exist "because of width." As an editorial rule of thumb, if there are three or more, it is worth moving them to container queries. If there are only one or two, it is fine to leave things as they are.

We checked the Smashing Magazine article and MDN's documentation through web searches on September 24, 2026. We have not tested the CSS behavior covered in this article on actual devices in each browser, nor applied it to existing sites. This article organizes the specifications and provides material for deciding on refactoring.

For CSS refactoring of an existing site or rebuilding components, please consult GleamHub.

Share this articleXFacebook
Kakeru Suzuki

Fascinated by the possibilities of technology, has had a deep interest in programming and digital art since student days

Turn this article's theme into your company's next step

Starting from what you want to achieve with your website.

We organize user goals, required features, and ongoing maintenance structures to determine the first steps in development and improvement.

  • Website objectives
  • Features and usability
  • Post-launch operations
Consult on web development and improvements

You can consult with us from the initial conceptual stage. Details from this article will be carried over to the inquiry form.

Receive the latest articles via email · Read the web production guide
Free download

Complete Guide to Web Production: Costs, Vendor Selection & Traffic Acquisition [2026 Edition]

We have compiled cost benchmarks, vendor selection criteria, and traffic acquisition strategies into a PDF.

The PDF and newsletter emails are currently in Japanese.

You will also be subscribed to our newsletter. You can unsubscribe at any time.