CSS cascade layers let you rank groups of rules: a rule in a later @layer beats a rule in an earlier one, whatever the specificity of either selector. Anything outside a layer beats them all. So when you add layers to an existing stylesheet, move the lowest-priority CSS in first (the reset, then third-party files) and your own last.
We ran every example below in Chromium 149, Firefox 151 and WebKit 26.5, the builds Playwright 1.61 drives, and all three agreed each time. The web-features data gives Chrome 99, Firefox 97 and Safari 15.4 as the first versions with support, and has marked the feature Baseline widely available since September 2024.
How do CSS cascade layers decide which rule wins?
The CSS Cascade 5 draft sorts conflicting declarations by a fixed list of criteria, and layers come before specificity in that list. Specificity and source order still settle a conflict between two rules in the same layer. Between layers, the layer order has already decided.
Layers take their order from the first time each name appears, so one statement at the top can set it:
@layer reset, vendor, components;
@import url("vendor.css") layer(vendor);
@layer reset {
a { color: inherit; }
}
@layer components {
.btn { color: green; }
}
Here vendor.css holds a single rule, .widget a.btn:not(.is-disabled) { color: red; }, standing in for a library that writes long selectors. Without the layers that selector (specificity 0-3-1) beats .btn (0-1-0) and the link is red. With them it is green, because components comes after vendor in the list.
Once set, the order stays put. A later @layer b, a; after @layer a, b; changed nothing in our runs, and reopening a layer with a second block added rules to it where it already sat. Keep the statement out of media queries, though. The draft says a layer declared inside a conditional rule counts only while the condition is true, and a stylesheet that named a layer inside @media (min-width: 600px) gave us opposite winners at 400 and 800 pixels wide.
Inline styles and !important sit around the layers like this, strongest first. The table covers a page’s own CSS and leaves animations and transitions aside.
| Rank | Where the declaration is |
|---|---|
| 1 | style attribute with !important |
| 2 | In a layer with !important, earliest layer first |
| 3 | Outside any layer with !important |
| 4 | style attribute |
| 5 | Outside any layer |
| 6 | In a layer, latest layer first |
Why does unlayered CSS beat every layer?
Unlayered CSS wins because the draft gives it a layer of its own at the end of the order. In its words, “any declaration not assigned to an explicit layer is added to an implicit final layer”, and for ordinary declarations the last layer wins. You cannot move that implicit layer either: as of December 2026 the draft still lists “Allow authors to explicitly place unlayered styles in the layer order” as an open issue.
A half-finished migration runs straight into it:
@layer vendor, components;
@layer components {
.widget a.btn { color: green; }
}
/* legacy.css, not yet in a layer */
a { color: blue; }
The link is blue. A bare element selector left outside now outranks the component rule that was just moved into a layer, and no selector inside the layer can win it back.
You can meet the same behaviour without writing @layer yourself. Tailwind CSS 4.3.5 opens its index.css with @layer theme, base, components, utilities;. We compiled @import "tailwindcss"; followed by an unlayered a { color: blue; } with the Tailwind CLI, and a link carrying text-red-500 came out blue. With the same rule inside @layer base, the utility won.
How do you put third-party CSS in a layer?
Use @import with layer(), as the first example does. @import url("vendor.css") layer(vendor); assigns every rule in the file to vendor, and a bare layer keyword gives the file an anonymous layer of its own.
MDN’s @layer reference gives the placement rule: an @import has to come before every other kind of rule except @charset and @layer statements, and an @layer block is not a statement. When we put an @layer reset { ... } block above the import, all three engines ignored the import.
We found no way to do this from HTML. We tried <link rel="stylesheet" href="vendor.css" layer="vendor"> and the file stayed unlayered, and the draft records “Provide an attribute for assigning link or style elements to cascade layers?” as another open issue. A stylesheet you load with <link> therefore has to be imported from a <style> block or another stylesheet, or wrapped at build time.
Bundlers can do the wrapping. Vite 8.4.1 built the first example into a single file with the vendor rule inside an @layer vendor block, and that file gave the same green link. Our Vite 8 migration notes cover the rest of that upgrade.
What does !important do inside a layer?
Inside a layer, !important reverses the order. The draft says “for important rules the declaration whose cascade layer is earliest wins”. A layered !important also beats an unlayered one.
@layer vendor, components;
@layer vendor {
.btn { color: red !important; }
}
@layer components {
.widget a.btn { color: green !important; }
}
#cta { color: blue !important; }
The link is red. The declaration in the earliest layer wins, though its selector is the least specific of the three.
While a library’s CSS is unlayered, your own !important can beat its !important with a more specific selector. Put the library in a low layer and its !important declarations outrank every !important in a later layer or outside one. So search the file for !important before you move it. If you have to beat one afterwards, declare a layer ahead of the library’s and put the override there: with @layer overrides, vendor, components;, an !important rule in overrides beat the one in vendor.
In what order should you migrate an existing stylesheet?
Migrate from the bottom of the order up. Whatever you move into a layer starts losing to everything still outside one, so each step should move the CSS that the rest is supposed to beat.
Declare the whole order first, for example @layer reset, base, vendor, components, utilities;. Names can be listed before they hold any rules. Then move the reset. Then move your bare element styles (a, h2, button) into base, and do it before the library goes in: when we imported vendor.css into a layer and left a { color: blue; } outside, the element rule beat the library’s component selector. Third-party files go next, after the !important search. Your own components come last, and they can stay unlayered for as long as you like, since unlayered CSS already acts as the top layer.
For the odd place where the lower layer was right, revert-layer hands the property back. A color: revert-layer declaration in components gave our link the vendor colour again.
Any of these steps can change which rule wins somewhere, so we would ship them one at a time and compare screenshots before and after.
When are layers not worth it?
We would add layers wherever CSS from more than one source meets: a reset, a library’s stylesheet, a design system, utility classes. Those are the places where one source’s selectors outrank another’s by accident, and a layer order states the intended ranking once. We would not bother on a small site where every rule is ours and styles are already scoped per component. We would not add a layer to win one stubborn override either. Layers only decide priority; @scope is the tool for limiting which elements a rule can reach.
We take over websites that someone else built, and the first thing we do is read the code and say plainly what state it is in before changing anything. If yours has a stylesheet nobody dares touch, that is the kind of job our website maintenance and support service is for.