Sub topic 7
Importing stylesheets into a layer with @import
This chapter assumes you know selector specificity, source order, and how to declare layers with @layer blocks and an order statement. Here you will put a whole external stylesheet into a layer without editing that file.
Why import into a layer
You often receive CSS you cannot or should not edit, such as a vendor library or a framework. Its selectors may be specific, and fighting them with !important or longer selectors gets messy. If you place the whole file in a layer, its rules take the priority of that layer. Your own later layers then win, whatever the specificity.
The layer() function
Add layer(name) after the URL in an @import rule:
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn { padding: 0.5rem 1rem; }
}Every rule in vendor.css now belongs to the vendor layer. The statement on the first line sets the order, so site comes after vendor and wins conflicts. A .btn rule in vendor.css with a higher specificity still loses to the site rule.
You can write @layer statements before @import. Other rules must not come before an @import, or the browser ignores the import. The one exception is @charset and other @layer statements.
How the order gets decided
Layer order is set by the first time each layer name appears. Compare two versions of the same code.
Without the order statement:
@import url("vendor.css") layer(vendor);
@layer site {
.btn { padding: 0.5rem 1rem; }
}Here vendor appears first, so it is the earlier layer. That works, but the order depends on the import line staying above the block. With an order statement at the top, you can move things around freely. Prefer the explicit statement, as the previous chapter showed.
Importing into a nested layer
The layer name in layer() can use dot notation. This puts the file inside a nested layer:
@layer framework, site;
@import url("ui-kit.css") layer(framework.base);The framework layer is created, and base is a sublayer inside it. Other rules can later reopen framework.base using the same name.
Anonymous layers
If you write layer with no name, the import goes into an anonymous layer:
@import url("legacy.css") layer;An anonymous layer cannot be referenced again, so nothing else can add rules to it. It is placed in the order at the point where it appears. Use a name when you may need to reopen or reorder it.
Imports and performance
Each @import makes the browser fetch another file, and the fetch only starts after the browser has parsed the importing stylesheet. Chains of imports slow down rendering. For production, you can instead use <link> elements or a build step that bundles the CSS. Layers are still available in bundled CSS: wrap the file's contents in an @layer block.
@layer vendor {
/* contents of vendor.css pasted here */
}Common errors
- Writing
layer(vendor)but never listingvendorin the order statement, so its position depends on where the import happens to sit. - Placing
@importbelow other rules, which makes the browser skip it. - Expecting the import to win because the vendor file has strong selectors. Layer order is compared before specificity.
- Putting
vendor.cssrules that use!importantin an early layer. As you will see in a later chapter, important declarations reverse the layer order, so the vendor layer can win those.
Exercise
You write @layer base, site;, then @import url("lib.css") layer(base);. lib.css contains #app .btn { color: red; }. Your own stylesheet has @layer site { .btn { color: blue; } }. What colour is a .btn inside #app, and why?
Show answer
Blue. The imported file's rules are in the base layer, and site is declared after base, so site wins. Layer order is checked before specificity, so the ID selector in lib.css does not help it.
Exercise
What happens if an @import rule appears after a normal style rule in a stylesheet?
Show answer
The browser ignores the @import. Only @charset and @layer statements may come before it.