Grandma Marni’s price card pinned in the narrow and wide columns of Mr. Karim’s board: write @container and @media, drag the board and sidebar widths, and the…
Why it matters
Components get reused everywhere: sidebars, grids, modals. With @media you only know the screen’s width, not the slot’s. @container lets a component read its own room, so it really works anywhere.
The key idea
A container query asks an ancestor, not the window
Common mistakes
Wrote @container but nothing happens Without container-type: inline-size on an ancestor, @container has no container to measure, so its query never matches. This is the step most often forgotten.
Querying a container from the container itself An element that is a container cannot query itself: the query looks for a container ABOVE the element. Make the wrapper the container, then style what is inside.
Using cqw/cqi without knowing what they are relative to Container units are relative to the element’s nearest container. With no container at all, they fall back to the (small) viewport size.
Try it in this playground
Drag the side column’s width until its card sits side by side Only the side card changes; the window does not move at all.
Remove container-type from .wrap @container finds no container, so its query never matches (a cross on both cards).
Open the @media situation, then drag the board width Both cards change together at 700px, including the one in the narrow side column.
With @container, drag the board width The main card changes because the main column grows, not because of the window.