Structure and files
Give every style one home. One entry file loads the layers in a fixed order, and each component owns exactly one stylesheet that no other file touches.
- Required 1
- Recommended 5
Required
Keep untrusted data out of stylesheets
CSS-35Pass user or third-party data to CSS only as the value of a custom property your code names, after it matches a fixed pattern. Such data never chooses a property, a selector or a whole declaration, and never reaches a
url().
Recommended
Load every stylesheet from one entry file
CSS-1Load all CSS through one entry file that imports every other stylesheet in a fixed order. Pages load that one built file and nothing else.
Sort styles into five layers
CSS-2Put every style you write in one of five layers, in the order tokens, base, layout, components and utilities. Each layer has its own folder, and a file belongs to exactly one layer. Third-party CSS sits in one
vendorlayer before them.Give each component exactly one stylesheet
CSS-3Keep all CSS for a component in one file named after it. No other file may select that component's classes, so a conflict is never settled by which file loads last.
Keep media and container queries with their component
CSS-4Write 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.
Write styles only in stylesheets
CSS-5Keep every style declaration in a stylesheet. Templates have no
styleattributes or<style>elements. Scripts set noelement.styleproperty except a custom property throughstyle.setProperty('--name', value). The one other exception is a<style>element the build writes from the bundled stylesheet.
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">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.