LayoutBaseline 2023
Container Queries
Respond to a container's own size instead of the viewport. Build truly portable components that adapt wherever they're placed.
- Use it when
- Components that live in more than one place: a card that sits in a narrow sidebar on one page and a wide grid on another, or a dashboard widget users can resize.
- Watch out
- Only an ancestor with container-type can be queried; an element can't query itself. container-type: inline-size also stops the container sizing itself from its content, so a shrink-to-fit container (a flex item with no width, say) can collapse to zero.
.wrapper {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 480px) {
.card {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}
}