Container queries and :has() in practice
Firefox 110 shipped size container queries in February, so they now work in every major browser.
Firefox 110 shipped size container queries in February, so they now work in every major browser. Together with :has(), which Chrome and Safari already support, they take care of two things we’ve been hacking around in CSS for years.
Container queries
Media queries look at the viewport. That’s fine for page layout, but a component doesn’t care how wide the window is. A product card in a narrow sidebar and the same card in a wide main column need different layouts, even on the same screen.
Container queries let the card respond to the space it actually has.
.card-list {
container-type: inline-size;
container-name: cards;
}
.card {
display: grid;
gap: 0.75rem;
}
@container cards (min-width: 32rem) {
.card {
grid-template-columns: 8rem 1fr;
}
}
There’s a shorthand too: container: cards / inline-size;.
The one rule to remember is that an element can’t query its own size, only an ancestor’s. That’s why container-type goes on the list and not on the card. If there’s no suitable parent, add a wrapper element.
When the container collapses
container-type: inline-size tells the browser that the element’s width doesn’t depend on its content. That’s what makes container queries possible without endless layout loops. The side effect is that a container inside something that sizes to its content, like a flex item without a width or an inline-block, can shrink to zero.
If a container suddenly vanishes, give it a width (width: 100% or flex: 1), or move the container to a block-level parent.
Container query units
There are new units relative to the container. cqi is 1% of the container’s inline size, cqw 1% of its width, and so on. They work nicely for type that scales with the component:
.card h3 {
font-size: clamp(1rem, 4cqi, 1.5rem);
}
:has()
:has() usually gets called the parent selector, but it’s more general than that. It selects an element based on what’s inside it, or on what comes after it.
A few things that used to need JavaScript:
/* cards with an image get a different layout */
.card:has(img) {
grid-template-rows: 12rem auto;
}
/* mark the whole field when its input is invalid */
.field:has(input:invalid:not(:placeholder-shown)) {
border-color: var(--error);
}
/* stop the page scrolling while a modal is open */
body:has(dialog[open]) {
overflow: hidden;
}
/* add an asterisk to labels of required inputs */
label:has(+ input:required)::after {
content: ' *';
}
The :not(:placeholder-shown) part stops fields from looking invalid before anyone has typed. It only works if the input has a placeholder.
Firefox doesn’t have it yet
This matters more than usual. If a browser doesn’t understand a selector, it drops the whole rule, including any other selectors in the same list. So keep :has() rules on their own, and wrap them in @supports:
@supports selector(:has(a)) {
.card:has(img) {
grid-template-rows: 12rem auto;
}
}
For now, treat :has() as an enhancement. The page should work without it and look better with it. For something that must work everywhere, like the scroll lock, keep a JavaScript fallback until Firefox catches up.
It’s also worth keeping the part before :has() specific. .field:has(…) gives the browser far fewer elements to re-check on every DOM change than something like *:has(…).
Both together
The two combine well. Here a card switches layout only when there’s room and it actually has an image:
.card-list {
container: cards / inline-size;
}
@container cards (min-width: 32rem) {
.card:has(img) {
grid-template-columns: 8rem 1fr;
}
}
That used to take a resize observer, an extra class from JavaScript, and a prop telling the card whether it had an image. Now it’s six lines of CSS, and the card works wherever you drop it.