Using CSS Container Queries for Component‑Level Responsiveness
Learn how CSS container queries let components react to their parent’s size, with a practical product‑card example, setup steps, limitations, and verification tips.
27 Nov 2025, 00:18 UTC

The problem: reusable components that ignore their surroundings
When you build a UI component like a product card, you often want it to change layout based on the space it actually occupies. Media queries look at the viewport width, so a card that looks fine on a wide desktop screen can break when placed inside a narrow sidebar or a grid column. The component has no way to react to its own container’s size.
Thesis: container queries let a component respond to its ancestor’s size
CSS container queries (size queries via container-type and @container) give you a way to write styles that depend on the width (or height) of a containing element, independent of the page viewport. This makes truly reusable components possible.
How to set up a containment context
- On the element that will act as the query container, declare
container-type: inline-size. This establishes size containment on the inline axis (the width in horizontal writing modes). - Optionally give the container a name with
container-name: your-nameif you need to disambiguate multiple nested containers. - Write
@containerrules that target descendants of that container, using the same syntax as media queries but prefixed with@container.
Worked example: a product card that adapts to its container width
Imagine a card that shows an image and some text. Below 400 px of container width we want a stacked layout; at 400 px or wider we switch to a horizontal flex layout.
<!-- HTML -->
<div class="card-container">
<article class="product-card">
<img src="product.jpg" alt="Product">
<div class="details">
<h3>Product Name</h3>
<p>Short description…</p>
</div>
</article>
</div>
/* CSS */
.card-container {
/* make this element a query container */
container-type: inline-size;
/* optional name: container-name: card; */
width: 100%; /* will be resized by a parent grid or sidebar */
padding: 1rem;
background: #f9f9f9;
}
.product-card {
display: flex;
flex-direction: column; /* default: stacked */
gap: 0.5rem;
}
.product-card img {
max-width: 100%;
height: auto;
}
/* When the container is at least 400px wide, switch to horizontal layout */
@container (min-width: 400px) {
.product-card {
flex-direction: row;
align-items: center;
}
.product-card img {
max-width: 150px; /* limit image size in row layout */
}
}
In this example the card’s layout changes based on the width of .card-container, not the browser window. You can test it by placing the container inside a resizable sidebar or a CSS grid column and adjusting that column’s width.
Trade‑offs and limitations
- Cannot style the container itself from inside its own
@containerblock; you must style descendants. - Size containment pitfalls: using
container-type: size(full width + height) without an explicit height can cause the element to collapse to zero height because it is removed from intrinsic sizing. Stick toinline-sizeunless you really need both axes and can define a height. - Fallback needed for older browsers: browsers that don’t support container queries will ignore the
@containerblock, so ensure the default styles (outside the block) provide a usable layout. - Naming complexity: deeply nested named containers can become hard to follow. Prefer the nearest‑ancestor unnamed query unless you truly need to disambiguate.
Actionable closing: verify and adopt
To verify that your container query works:
- Open the page in a modern browser (Chrome ≥ 105, Firefox ≥ 110, Safari ≥ 16).
- Right‑click the container element and choose “Inspect”. In the Elements panel you should see a badge indicating it is a query container.
- Resize the container (e.g., drag a sidebar splitter or adjust a grid column’s width) and watch the card’s layout change at the 400 px breakpoint.
- Disable container queries in DevTools (under the “Experiments” or “Features” pane) or test in an older browser to confirm the fallback layout still looks reasonable.
With these steps you can confidently use container queries to build components that adapt to their immediate surroundings, keeping your CSS modular and your layouts robust.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.