Use CSS container queries for component-level responsive design
Media queries look at the viewport. But a component does not care how wide the window is, it cares how much space it has: a post card in a narrow sidebar and the same card in the wide main grid need different layouts at the same viewport width. Container queries let a component adapt to the size of its container. Since early 2023 they work in all current browsers (Chrome 105, Safari 16, Firefox 110).
Problem: A post card is used in a three-column grid, in a narrow sidebar widget and in a full-width featured section. Each context has its own CSS class with duplicated rules and media queries tied to viewport widths, which break when the sidebar width changes.
Solution: Make the wrappers containers with container-type: inline-size and write the card’s layout changes with @container instead of @media. One component, no extra markup, no JavaScript.
Declare the containers
.post-grid,
.sidebar-widget,
.featured-section {
container-type: inline-size;
}
/* A name lets a rule target one specific kind of container */
.sidebar-widget {
container-name: sidebar;
}
inline-size means the container’s width can be queried. The card itself is not the container: an element cannot query its own size, so the container is always a wrapper.
The card
/* Base: image above the text */
.post-card {
display: grid;
gap: 0.75rem;
background: var(--ha-card-bg, #fff);
border-radius: 0.5rem;
overflow: hidden;
}
.post-card__thumbnail {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}
.post-card__title { font-size: 1rem; }
.post-card__excerpt { font-size: 0.875rem; }
/* At least 480px of space: image on the left */
@container (min-width: 480px) {
.post-card {
grid-template-columns: 200px 1fr;
}
.post-card__thumbnail {
grid-row: span 3;
height: 100%;
aspect-ratio: auto;
}
}
/* In a narrow sidebar: small thumbnail, no excerpt */
@container sidebar (max-width: 300px) {
.post-card {
grid-template-columns: 80px 1fr;
gap: 0.5rem;
}
.post-card__thumbnail { aspect-ratio: 1; }
.post-card__excerpt { display: none; }
}
/* The title scales with the container, not with the viewport */
@container (min-width: 600px) {
.post-card__title {
font-size: clamp( 1.125rem, 3cqi, 1.5rem );
}
}
An unnamed @container rule uses the nearest ancestor container. cqi is 1% of the container’s inline size (there are also cqw, cqh, cqb, cqmin, cqmax), so the title grows with the space the card has.
The catch: size containment
A container with container-type: inline-size gets size containment on that axis: its width cannot depend on its content. In a normal block or a grid cell this is not noticeable, because the width comes from the parent. But a container that should shrink to fit its content (a float, an inline-block, a flex item with flex: 0 0 auto) collapses to zero width. Give such a wrapper a width from outside, or put the container one level higher.
NOTE: Container queries do not replace the page grid: the overall layout is still defined with CSS Grid and media queries (see CSS Grid basics), and the components inside adapt with container queries. Custom properties work well with both, see CSS custom properties.