Build Responsive Components with CSS Container Queries
Learn how container queries let a component respond to the space available in its own layout, with practical setup steps, fallback guidance, and debugging tips.
A card that looks balanced in a wide content area can become cramped in a sidebar. Traditional media queries respond to the viewport, but a component’s available space is often determined by its parent. CSS container queries let you adapt the component to that local space instead.
This is especially useful for reusable cards, navigation panels, product summaries, and dashboard modules. Instead of asking whether the screen is wide, the component can ask whether its container is wide enough for a different layout.
How a container query works
A queryable container needs a containment context. For width-based layout changes, set container-type: inline-size on the element whose available inline space should control its descendants. Then use an @container rule to change styles when that container meets a condition.
Example: .product-panel { container-type: inline-size; } .product-card { display: grid; grid-template-columns: 1fr; gap: 1rem; } @container (min-width: 38rem) { .product-card { grid-template-columns: 10rem 1fr; align-items: center; } }
In this example, the panel establishes the query context. The card begins as a single-column layout. Once the panel is at least 38rem wide, the card places its image and details side by side. If the same card appears in a narrow sidebar, it can remain stacked even on a large monitor.
Set it up in four practical steps
-
Choose the right container. Put containment on the layout region that controls the component’s available width, not automatically on the component itself. A parent wrapper is often the clearest choice.
-
Start with the compact layout. Define a readable single-column version first. This gives the component a sensible baseline in narrow spaces and simplifies the query rule.
-
Add one meaningful breakpoint. Pick a width at which the design can genuinely support a new arrangement. The right value depends on the content, gaps, and minimum useful size of each column.
-
Test in different contexts. Check the component in a full-width region, a sidebar, and any nested layout where it will be reused.
Choosing between media and container queries
| Use this | When the design should respond to | Typical example |
|---|---|---|
| Media query | The viewport or a device-level preference | Changing the page navigation at a screen breakpoint |
| Container query | The space available to a component | Rearranging a card inside a sidebar or grid cell |
These tools can work together. A media query can change the page’s columns, while a container query lets each card respond to the width its new column provides. Avoid using a container query merely because it is newer; use it when the component should be independent of where it is placed.
Common pitfalls and checks
- Confirm the query context. A query evaluates against an eligible ancestor container. If a rule appears to do nothing, verify that the intended ancestor has the container property and is actually above the queried element.
- Watch intrinsic sizing. Inline-size containment changes how an element’s size is calculated. Give the container an appropriate size through its layout context, and check for unexpected shrinkage or overflow.
- Do not guess breakpoint values. Resize the container gradually and note where text wraps, controls collide, or columns become too narrow. Set the threshold just before the alternate layout becomes useful.
- Keep a robust baseline. Modern browsers broadly support container queries, but a simple default layout remains valuable. Where older browser support is required, consider a media-query fallback or retain the stacked layout as the usable default.
Frequently asked questions
Can a container query change the container itself?
Size queries are generally intended to style descendants based on a qualifying ancestor’s size. Put the conditional styles on the elements inside the container, and use the page or parent layout to size the container.
Should every reusable component have a container query?
No. Add one when the same component needs different layouts in different-sized regions. If it behaves well at all widths, extra query rules add complexity without improving the result.
Make the component placement-aware
Container queries are most effective when they encode a component’s actual layout needs: what fits, what remains readable, and when a different arrangement helps. Establish a compact baseline, query the space that matters, and test the component in its real parent layouts. That keeps responsive behavior close to the component instead of tying it to assumptions about the whole screen.