WEB-29Recommended
Let parts fit the space they get
Let components adapt to the space they are given with container queries, not only to the width of the screen.
How to do it
Set container-type: inline-size on a component's wrapper. Write @container (max-width: …) rules for what is inside it. Check the result as Never make people scroll sideways says.
Examples
CSS example
.card-wrap { container-type: inline-size; }
.card { display: grid; grid-template-columns: 8rem 1fr; gap: 1rem; }
@container (max-width: 30rem) {
.card { grid-template-columns: 1fr; }
}
How to check
A browser test renders the component in a 300 px and a 900 px wide container. Neither scrolls sideways, and the layout changes between them.
Sources
Related rules
- AXS-8 Never make people scroll sideways in Accessibility
- CSS-4 Keep media and container queries with their component in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/interface/container-queries">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.