CSS-4Recommended
Keep media and container queries with their component
Write every media or container query inside the stylesheet of the component it changes. Never collect them in a separate file for screen sizes or print.
How to do it
A file such as responsive.css overrides components from the outside, so it breaks the one-owner rule. Put the query next to the styles it changes. Prefer container queries, so the component reacts to its own space, as Size components with container queries describes. A query inside a component file only selects classes with that file's prefix. The same goes for user preference queries. A component's prefers-reduced-motion and forced-colors blocks sit in its own file. What they must do is set by Respect people who turn motion off and Work in high-contrast mode.
Examples
CSS example
/* components/order-card.css: the query lives with the card */
.order-card { container-type: inline-size; }
@container (min-width: 30rem) {
.order-card-body { display: grid; }
}
HTML example
<article class="order-card">
<div class="order-card-body">Two sourdough loaves, ready at 9.00</div>
</article>
How to check
No stylesheet exists whose name contains responsive, mobile, print or breakpoints. Inside each component file, every selector within @media or @container starts with the file's prefix.
Related rules
- WEB-29 Let parts fit the space they get in Website
- AXS-41 Respect people who turn motion off in Accessibility
- AXS-3 Work in high-contrast mode in Accessibility
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/html-css/structure-and-files/queries-with-the-component">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.