What are CSS container queries and when should you use them?

CSS container queries let a component change styles according to the size or features of an ancestor container instead of the viewport. They are useful when the same component can appear in a narrow sidebar, a wide content area or a resizable panel. The component responds to the space it actually receives.
Declare a query container, usually with container-type: inline-size, then write an @container rule. The query searches ancestors for an eligible container; it does not normally query the component itself because styling it based on its own size could create circular layout decisions.
.card-region { container-type: inline-size; }
@container (min-width: 30rem) {
.card {
grid-template-columns: 8rem 1fr;
}
}
Use container queries for cards, navigation modules, product panels and other reusable components whose layout depends on placement. Use media queries for page-level conditions such as viewport-wide navigation, reduced motion, color scheme, hover capability or print output.
Choose thresholds from the component’s content and test every supported container. Add a container name when nested containers make the selected ancestor unclear. Container query units such as cqi can support fluid sizing, but they still need minimums and maximums to prevent extreme text or spacing. Keep the rule beside the component it controls and document the reason for the threshold or constraint. This makes future changes easier to evaluate and prevents an unrelated page stylesheet from silently overriding the responsive behavior. Test the solution with keyboard focus, touch input and a resized desktop window. Responsive geometry is only successful when every visible control remains reachable and the content keeps a logical reading order.