Agent Wranglers

CSS-1Recommended

Load every stylesheet from one entry file

Load all CSS through one entry file that imports every other stylesheet in a fixed order. Pages load that one built file and nothing else.

How to do it

Keep a file such as styles/main.css that only holds imports, in layer order. The build bundles it into one file with a content hash in its name. Templates never add their own <link rel="stylesheet">. A site that inlines its first-paint CSS, as Put the styles in the page says, has the build cut that part from the same bundle. The order of the imports follows Sort styles into five layers, and the cascade order is set once by Declare the layer order once.

Examples

CSS example

/* styles/main.css: imports only, in layer order */
@layer tokens, base, layout, components, utilities;

@import url("tokens/index.css") layer(tokens);
@import url("base/index.css") layer(base);
@import url("layout/index.css") layer(layout);
@import url("components/index.css") layer(components);
@import url("utilities/index.css") layer(utilities);

HTML example

<!-- Every page of the Harbour Bakery site links the same one file -->
<link rel="stylesheet" href="/assets/main.4f9a2c.css">

How to check

Every built page has at most one <link rel="stylesheet">, and it points to the bundled entry file. A page with no link has the whole bundle in its one build-written <style> element. The entry file holds only @layer and @import statements.

Sources

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/one-css-entry-file">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.