CSS Container Queries
Implement responsive components using CSS container queries that adapt based on parent container width not viewport.
Code
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (min-width: 400px) {
.card {
grid-template-columns: 1fr 1fr;
}
}CSS Container Queries
Implement responsive components using CSS container queries that adapt based on parent container width not viewport.
Code
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (min-width: 400px) {
.card {
grid-template-columns: 1fr 1fr;
}
}
Explanation
A component reflows from the width of its parent container rather than the viewport, so the same card adapts inside a narrow sidebar and a wide main column. Expected behavior: the @container rules apply once the nearest container meets the size condition. Constraint: the ancestor must be declared a container with container-type, otherwise the query never matches.
Interactive Tools for This Topic
Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.CSS clamp() GeneratorGenerate responsive clamp(MIN, PREFERRED, MAX) values with automatic calc() wrapping for expressions.Viewport Unit CalculatorConvert between pixels and the CSS viewport units vw, vh, vmin, and vmax for a given viewport size, in either direction.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.
Report a problem with this exampleOpens a pre-filled issue on GitHub.