How to Reflow a Card at 480 Pixels Based on Its Slot Width, Not the Screen: A Short CSS @container RecipeBrief · digest for November 27
Brief≈ 3 min

How to Reflow a Card at 480 Pixels Based on Its Slot Width, Not the Screen: A Short CSS @container Recipe

You style a feed card—image on the left, text on the right—and it looks great. Drop that same card into a narrow sidebar on a wide monitor, and the layout breaks. Standard @media rules check the browser viewport rather than the column, forcing teams into .card--compact modifier classes or JS ResizeObserver hacks.

Modern CSS solves this cleanly with container queries (@container), which measure a parent wrapper's width. Drag the slider below to watch the card switch layouts at 480 pixels.

Live Demo: Card Reflow by Slot Width (@container)

Two gotchas trip people up most often. First, you need the outer .card-slot wrapper: @container rules only apply to descendants, so placing it on .card itself won't work. Second, write inline-size, not size. inline-size tracks width only, letting height grow naturally with content. Using size detaches height from content, collapsing the container to zero without an explicit height. Naming the container card prevents accidental matches from nested containers.

Both @container and container-type: inline-size have been Baseline Widely available since February 2023 (Chrome/Edge 105+, Firefox 110+, Safari 16.4+), with no polyfills needed. Just remember that inline-size creates a stacking context and a containing block for position: fixed descendants.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2