c81b4faarticle1 min read
Container queries: field notes
Components that respond to the space they are given, not the size of the window. What works today and what to watch for.
Published
Media queries ask how wide the viewport is. Container queries ask how wide the component's parent is — which is almost always the question you meant.
The minimum setup
.card-grid {
container-type: inline-size;
}
@container (min-width: 36rem) {
.card {
grid-template-columns: 12rem 1fr;
}
}Things that catch people out
- An element cannot query itself. Put
container-typeon a wrapper. inline-sizecontainment means the container no longer sizes to its content horizontally. Give it a width from its own parent.- Container query units such as
cqiare relative to the nearest container, which makes fluid type inside components straightforward.