Sub topic 7
Importing stylesheets into a layer with @import
This chapter assumes you can already write @layer blocks and order statements, and that you know the layered-versus-unlayered rule. It also assumes you can read applied styles in DevTools. Here you will learn how to put a whole external stylesheet into a layer without editing that file.
Why import into a layer
Third-party CSS such as a UI kit or a reset is often written with specific selectors like .toolbar .btn. Overriding it used to mean matching or beating that specificity. If you place the whole file in an early layer, every rule in it loses to any rule in a later layer, whatever the selectors look like.
The layer() function on @import does this. The imported file stays untouched, and the browser treats its rules as if they were written inside an @layer block.
Basic syntax
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn { padding: 0.5rem 1rem; }
}Suppose vendor.css contains this:
.toolbar .btn { padding: 4px 8px; background: #ddd; }The vendor selector has higher specificity than .btn. The site rule still wins, because site is declared after vendor and layer order is checked before specificity.
The first line is an order statement. @layer statements are allowed before @import, so you can fix the layer order before any file loads. That means the order does not depend on which file finishes downloading first.
What happens to the imported rules
The imported file is treated as the contents of a layer block. This has several consequences.
- Unlayered rules in the file go into the named layer. Every plain rule in
vendor.cssis now invendor. - Layers declared inside the file become nested layers. If
vendor.csscontains@layer reset, theme;, you getvendor.resetandvendor.theme. - Several imports can target the same layer. Their rules are appended in the order the imports appear.
- Rules from other
@layerblocks of the same name join the same layer. A later@layer vendor { ... }block in your own file adds to it.
@layer vendor, site;
@import url("grid.css") layer(vendor);
@import url("buttons.css") layer(vendor);
@layer vendor {
.btn { border-radius: 0; }
}All three sources end up in vendor. Within that layer, normal source order and specificity still apply. buttons.css comes after grid.css, and the block comes last.
Modifiers and their order
@import accepts several optional parts after the URL. They must appear in a fixed order.
| Position | Part | Purpose |
|---|---|---|
| 1 | `url("file.css")` or a string | The stylesheet to load |
| 2 | `layer` or `layer(name)` | Put the file in a layer |
| 3 | `supports(condition)` | Load only if the browser supports the feature |
| 4 | Media query list | Load only when the media condition matches |
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@import url("grid-extras.css") layer(vendor.grid) supports(display: grid);
@import url("print.css") layer(vendor.print) print;The second import uses a dotted name. It creates a layer named grid nested inside vendor. The browser skips the third import unless the output is a printed page. If a condition fails, the file's rules are not applied, but the layer name is still registered in the order. Nothing is added to it.
Anonymous imports
Writing layer with no name creates an anonymous layer.
@import url("legacy.css") layer;Anonymous layers cannot be referenced later. You cannot add rules to them or reorder them in a statement. Their position comes from where the import appears. Use one only when you are sure nothing else will ever need to join that layer. In most projects a named layer is easier to debug.
Import placement rules
@import has an old restriction that matters even more with layers. It is only valid before every other rule, with two exceptions: @charset and @layer statements (the form with no block). Any @import that comes after a normal rule or an @layer block is silently ignored.
@layer base {
a { color: blue; }
}
/* Ignored: this follows a rule, so the browser drops it. */
@import url("late.css") layer(base);The fix is to move imports to the top and put the order statement before or among them.
@layer base, components;
@import url("late.css") layer(base);
@layer base {
a { color: blue; }
}Layer order comes from first mention
Layers are ordered by the first time their name appears, in either a statement, a block, or an import. This is why a leading order statement matters.
Without one, this code orders layers by import position:
@import url("site-base.css") layer(base);
@import url("framework.css") layer(framework);Here base is lower priority than framework, which is probably the opposite of what you wanted. A single statement fixes it:
@layer framework, base;
@import url("site-base.css") layer(base);
@import url("framework.css") layer(framework);Now base is later and wins. The order of the import lines no longer matters for priority.
Effects on !important
Importing third-party CSS into a layer interacts with the reversal rule from the previous chapter. For !important declarations, earlier layers beat later ones. If the vendor file uses !important, it now wins over the same property declared !important in your later layers.
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn { color: white !important; }
}If vendor.css has .btn { color: black !important; }, the button text is black. The vendor layer comes first, and for important declarations the first layer wins. Normal declarations in site still beat normal declarations in vendor. Important ones go the other way.
You have two ways to handle this. One is to write a wrapper in a layer placed even earlier, for example @layer overrides, vendor, site;, and put your important overrides there. The other is to avoid !important in your own code and accept the vendor's choice.
Practical considerations
- Imports block parallel loading. A browser must fetch a CSS file before it finds the
@importlines inside it, so chained imports load in sequence. For production, bundle files or use a build step that inlines imports and keeps thelayer()part. - A
linkelement has no layer attribute. HTML cannot place a linked stylesheet into a layer. To layer a linked file, link a small entry stylesheet that imports it, or import it from your own CSS. - Cross-origin files work. You can import a CDN stylesheet into a layer. The layering is applied by the importing sheet, so the remote file does not need to opt in.
- The imported file's own
@importlines are also layered. They land inside the layer you chose, nested as above. - Unlayered import means unlayered rules.
@import url("vendor.css");with nolayer()leaves the file's rules unlayered, so they beat every layer you have declared.
A complete example
Here is a project entry file. It loads a reset, a vendor kit, and your own code in a fixed order.
@layer reset, vendor, base, components, utilities;
@import url("reset.css") layer(reset);
@import url("ui-kit.css") layer(vendor);
@import url("base.css") layer(base);
@layer components {
.card { border: 1px solid #ccc; padding: 1rem; }
}
@layer utilities {
.hidden { display: none; }
}If ui-kit.css has body .card.featured { padding: 3rem; }, your .card rule in components still beats it. The .hidden utility beats any component rule of any specificity. The kit stays upgradable, because you can replace ui-kit.css with a newer version and your overrides keep working.
Troubleshooting checklist
- Is the
@importbefore all rules except@charsetand@layerstatements? - Does the layer name in
layer()match the name in your order statement? A typo creates a new layer at the end of the order, which makes it the highest priority. - Does the file load at all? Look for a failed request in the Network panel.
- Is the property marked
!importantin the vendor file? If so, think about reversed order. - Is some other rule unlayered? Unlayered normal rules beat all layers.
- In DevTools, does the Styles pane show the layer name next to the rule? Struck-through rules from a lower layer confirm that layer order is the reason.
Exercise
A stylesheet contains @layer base { a { color: blue; } } followed on the next line by @import url("theme.css") layer(base);. The theme styles never apply. What is wrong, and how do you fix it?
Show answer
The @import follows an @layer block, so the browser ignores it. Move the @import above the block. Use a statement such as @layer base; first if you want to fix the order, then @import url("theme.css") layer(base);, then the @layer base { ... } block.
Exercise
Given @layer vendor, site; and @import url("kit.css") layer(vendor);, kit.css contains .btn { color: black !important; }. Your @layer site { .btn { color: white !important; } } comes later. What color is the button text, and why?
Show answer
Black. For !important declarations the layer order is reversed, so the earlier vendor layer beats the later site layer.
Exercise
main.css begins with @import url("a.css") layer(zeta); and @import url("b.css") layer(alpha); and has no order statement. Which layer has higher priority for normal declarations, and what single line would let you swap the priority?
Show answer
alpha has higher priority, because it is introduced later and later layers win. Adding @layer alpha, zeta; above the imports makes zeta the higher-priority layer.