Sphere
Log in Join

Using CSS container queries in production: the sizing traps I hit

I have been putting off container queries for about a year. Every time they came up I told myself browser support was not there yet, which was mostly true while Safari 15 was still in the mix. Then we dropped support for anything older than Safari 16 and Chrome 105, and I finally had no excuse.

Our problem was a card component that got dropped into three different layouts: a two-column settings page, a narrow sidebar in the admin shell, and a full-width dashboard tile. Media queries were doing the job badly because the breakpoint depended on the card's parent, not the viewport. I had a Card--compact prop being passed around like a hot potato.

## What we actually changed

The migration itself was small. We deleted the modifier prop and wrote the query against the grid cell:

css
.card-slot {
container-type: inline-size;
container-name: card;
}

@container card (max-width: 420px) {
.card__meta { display: none; }
.card__title { font-size: 1rem; }
}

That is the version that ended up in the PR. The first version did not work at all, and the reason was that I put container-type: inline-size on the card itself. A container cannot query itself. The query looks for the nearest ancestor with a containment context, so the thing you size has to live one level above. Took me an embarrassingly long time to see because the DOM looked fine in the inspector.

## The build error nobody warned me about

We run stylelint 15.10 in CI with the standard config, and it has no idea what @container is:

src/components/card.css
24:1 ✖ Unexpected unknown at-rule "@container" at-rule-no-unknown
31:3 ✖ Unexpected unknown at-rule "@container" at-rule-no-unknown

Two red X marks and a failed pipeline on a Friday afternoon. The fix is not to disable the rule globally. Add the at-rule to the config so you still catch typos everywhere else:

json
{
"rules": {
"at-rule-no-unknown": [true, {
"ignoreAtRules": ["container", "layer"]
}]
}
}

If you are on the scss syntax use scss/at-rule-no-unknown instead, with the same ignoreAtRules shape. I found that out after the first config change did nothing.

## The quiet part: containment changes layout

This is the bit that actually bit us. container-type: inline-size implies contain: layout inline-size style. That is not a no-op. Our cards had an absolutely positioned tooltip that was escaping the card and rendering over a table header. Once the slot became a container, the tooltip got clipped to the slot and looked broken. Nothing in the console, just a visual diff in a screenshot test that I nearly marked as an expected change.

The fix was to move the tooltip into a portal at the document body and position it with the trigger's bounding rect. Annoying, but honestly the right call anyway.

Also worth knowing: container-type: inline-size means the element's inline size no longer depends on its contents. If you relied on a grid column shrinking to fit the card's text, you will get a surprise. We had to put width: fit-content back on one inner wrapper to keep a small filter chip row from stretching.

## Where I landed

I still like them. The component no longer knows anything about where it is rendered, and the CSS reads closer to the design. But they are not a drop-in replacement for media queries, and the containment side effects are the part people leave out of the blog posts. Run your visual regression suite before and after, not just after.