Agent Wranglers

CSS-12Recommended

Import third-party CSS into its own layer

Import every third-party stylesheet into a named layer placed before your own layers. Never edit a vendor file in place.

How to do it

Add a vendor layer at the start of the declared order and import the package file into it with layer(vendor). Your base and component layers then win without higher specificity or !important. Change a vendor style by overriding it in your own layer, so an update of the package never loses your changes. Layers reverse for !important, so a vendor !important beats every style of yours, even your own !important. Change such a style through an option the vendor offers, or pick another package. The order is declared as in Declare the layer order once.

Examples

CSS example

@layer vendor, tokens, base, layout, components, utilities;
@import url("date-picker/dist/date-picker.css") layer(vendor);

/* components/date-picker.css: your override, in your own layer */
.date-picker-day { cursor: pointer; }

HTML example

<!-- One built file. The vendor styles sit in the lowest layer inside it. -->
<link rel="stylesheet" href="/assets/main.4f9a2c.css">

How to check

Every @import of a file from a package folder carries layer(vendor). The repository holds 0 copies of vendor CSS files, because each one is read from the package folder. A build test lists every !important in the imported vendor files, and the list matches a reviewed allow list.

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