Sub topic 1
Where Cascade Layers Fit in the Cascade
Cascade layers add one new step to the process the browser uses to pick a winning CSS declaration. This chapter explains that process and shows where layers sit in it. The later chapters cover the syntax and patterns in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic CSS syntax: selectors, properties, and declarations.
- How selector specificity is calculated, and that a later rule wins when specificity is tied.
- How to link or import a stylesheet.
- How to open browser DevTools and read the Styles panel for an element.
You do not need to know @layer yet. The chapter introduces only enough of it to make the examples readable.
The problem: specificity fights
Suppose a team's stylesheet contains this rule for navigation links:
#nav a.link { color: blue; }Later, someone adds a small helper class to turn text red:
.red { color: red; }The markup is <a class="link red"> inside <nav id="nav">. The link stays blue. The first selector has one ID and two other parts, so its specificity is (1, 1, 1). The helper class has only (0, 1, 0). The helper never wins, however late in the file it appears.
The usual workarounds are longer selectors, !important, or moving rules around until the page looks right. Each one makes the next override harder. Cascade layers address this by letting you decide which group of rules takes priority, before specificity is considered at all.
What the cascade does
The cascade is the algorithm that decides which declaration applies when several of them target the same property on the same element. It runs per property, not per rule. One rule can win for color and lose for padding.
The browser first collects every declaration that matches the element. It then compares them using a fixed sequence of criteria. It checks the first criterion, and only if two declarations tie on it does it move to the next.
| Step | Criterion | What decides it |
|---|---|---|
| 1 | Origin and importance | Where the declaration comes from, and whether it uses `!important` |
| 2 | Context | Shadow DOM boundaries (`:host` and outer styles) |
| 3 | Element-attached styles | The `style` attribute |
| 4 | Layers | The cascade layer the declaration belongs to |
| 5 | Specificity | The selector's (ID, class, type) weight |
| 6 | Order of appearance | The later declaration in source order |
Cascade layers are step 4. They come after origin, importance, and the style attribute. They come before specificity and source order.
Each arrow means "only if the previous step ended in a tie." A declaration that wins at step 1 is never compared on specificity.
Step 1: origin and importance
Every declaration has an origin, which says who wrote it:
- User-agent origin: the browser's built-in default styles, such as the blue underlined links and the margin on
body. - User origin: styles the person using the browser has set, for example through accessibility settings.
- Author origin: the CSS you write and ship with your site.
Each declaration is also either normal or !important. For normal declarations, the priority runs from lowest to highest as user-agent, user, then author. That is why your stylesheet can override the browser's defaults.
For !important declarations the order reverses: author important is the lowest, then user important, then user-agent important. This lets a user's !important accessibility setting beat the site's own !important. Transitions and animations also have their own origin positions, which this chapter does not cover.
The important point for this chapter is that all cascade layers live inside the author origin. Layers reorder author CSS against other author CSS. They never let a page override a user's !important rule or the browser's important defaults.
Steps 2 and 3: context and the style attribute
Context matters only with Shadow DOM. When a conflict crosses a shadow boundary, the cascade picks a winner based on which side is the outer context and on whether the rules are normal or important. Most pages never see this step.
Element-attached styles are declarations in a style="..." attribute. For normal declarations they beat any selector-based rule in the author origin, whatever its layer. This is why inline styles are hard to override from a stylesheet. A layer cannot beat a normal inline style, because the comparison ends at step 3.
Step 4: layers
A cascade layer is a named group of rules. You declare layers and give them an order. When two normal declarations reach step 4 tied, the one in the later layer wins. Specificity inside the layers is not looked at.
Here is the earlier problem, solved with layers:
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}The first line lists the layers in order, from lowest to highest priority. The two blocks put each rule into a layer. For <a class="link red"> inside #nav, both rules match and tie through steps 1 to 3. At step 4, utilities is later than base, so .red wins. The ID selector's higher specificity is never consulted.
The block and order forms are explained in the next two chapters. Here you only need to see that the layer decides before specificity does.
Step 5 and 6: specificity and order
Specificity and source order still matter. They apply when two declarations are in the same layer. Inside base, #nav a.link still beats a plain a rule. Inside utilities, a later rule beats an earlier one of equal specificity.
So layers do not replace specificity. They add a coarser level of control above it. You use layers to decide which group wins, and you use specificity and order to settle conflicts inside a group.
A worked comparison
This example shows the same two rules with and without layers. The rules target one button:
/* Version A: no layers */
.card button.primary { background: navy; }
button { background: gray; }In version A, .card button.primary has specificity (0, 2, 1) and button has (0, 0, 1). The navy rule wins no matter where each appears.
/* Version B: the weaker rule sits in a later layer */
@layer components, overrides;
@layer components {
.card button.primary { background: navy; }
}
@layer overrides {
button { background: gray; }
}In version B, the gray rule is in overrides, the later layer, so it wins. The strong selector loses because its layer has lower priority. This is the behavior that makes layers useful. It is also the main source of confusion when a rule you expected to win does not.
Layers cover the whole author origin
Layered and unlayered author styles compete with each other in the same origin. A declaration that is not in any layer is treated as belonging to an implicit final layer, which sits above all named layers for normal declarations. So a plain rule outside any @layer beats a layered rule. The chapter on unlayered styles explains the consequences and shows how to organize a project around them.
Keep this in mind from now on: adding @layer to some of your CSS changes how that CSS competes with the rest, even if no rule text changes.
Checking the cascade in DevTools
When you debug, work through the steps in order:
- Check origin and importance. Is a user-agent default or an
!importantrule involved? - Look for a
styleattribute on the element. - Look at the layer of each competing rule. Chrome, Edge, Firefox, and Safari show the layer name next to the rule in the Styles panel.
- If the competing rules share a layer, compare specificity, then order.
Skipping step 3 is the usual mistake. People compare specificity first, then wonder why the numbers do not explain the result.
Summary of the rules to remember
- The cascade compares origin and importance first, then context, then the style attribute, then layers, then specificity, then source order.
- Layers are an author-origin feature. They only reorder your own CSS.
- For normal declarations, a later layer beats an earlier one regardless of selector specificity.
- Specificity and source order still decide conflicts between rules in the same layer.
- Unlayered author styles are treated as a final layer that outranks all named layers for normal declarations.
Exercise
A page has this CSS. The markup is <p id="intro" class="note">. What color is the paragraph, and which cascade step decides it?
@layer base, theme;
@layer base { #intro { color: green; } }
@layer theme { .note { color: purple; } }
Show answer
The paragraph is purple. Both rules match and tie on origin, importance, context, and the style attribute. At step 4 the theme layer is later than base, so .note wins. The ID selector's higher specificity is never compared, because specificity is step 5.
Exercise
Put these in order from the first criterion the browser checks to the last: specificity, origin and importance, order of appearance, layers, the style attribute.
Show answer
Origin and importance, the style attribute, layers, specificity, order of appearance. Context (Shadow DOM) falls between origin and the style attribute, but it is not in this list.
Exercise
Two rules are in the same layer. One is .btn and appears first. The other is button and appears later. Which wins for color, and why?
Show answer
.btn wins. Within one layer, the layer step is a tie, so the browser compares specificity. .btn is (0, 1, 0) and button is (0, 0, 1), so the class selector wins even though it appears earlier. Source order is only used when specificity is also tied.