container-type
Turns an element into a queryable container.
Container queries let a component respond to the size of its parent container instead of the entire viewport.
.card-wrap { container-type: inline-size; }
@container (min-width: 34rem) {
.card { grid-template-columns: 12rem 1fr; }
}
Responsive mental model
Viewport media queries ask how wide the browser is. Container queries ask how much space this component actually has.
That matters because the same card might appear in a sidebar, a grid, a modal or a full-width section. The viewport can be large while the component is still narrow.
Container queries are excellent for reusable components because the component can adapt to its own available space.
Turns an element into a queryable container.
Applies rules when the container meets a condition.
Most component queries care about width.
Components adapt wherever they are placed.
Visual model
The parent provides the measured space.
The component stays compact in a narrow container.
The same component changes when the container grows.
The rule travels with the component.
Good CSS versus fragile CSS
.course-card-wrap { container-type: inline-size; }
.course-card { display: grid; gap: 1rem; }
@container (min-width: 34rem) {
.course-card { grid-template-columns: 12rem 1fr; }
}
@media (min-width: 900px) {
.sidebar .course-card { display: block; }
.main .course-card { grid-template-columns: 12rem 1fr; }
}
Rules of thumb
Cards, teasers, panels and widgets benefit from container queries.
A container query needs an ancestor with container-type.
Width-based component changes are the common case.
Viewport queries still make sense for large page structure.
Know which ancestor is the container being queried.
Place the component in narrow and wide contexts.
Production thinking
Container queries reduce layout hacks because components can make decisions based on their real space.
Container queries should not change meaning or source order. They should improve readability and usability in each context.
Use @supports if you need a careful fallback strategy for older browsers.
Container queries preserve one semantic component instead of duplicate markup for different placements.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, resize the lab idea mentally and explain what should change.
Senior audit upgrade
A component can respond to its own available space, but only if a parent establishes containment.
Name containers when a component needs to query a specific ancestor.
cqw, cqh and related units can size things relative to the query container.
Keep a usable default layout before enhancing with @container.