# CSS cascade layer

## What it is

A CSS cascade layer groups rules and gives those groups an explicit position in the author stylesheet's cascade. The stylesheet can declare an order with `@layer reset, vendor, components, theme;` and place rules in those layers wherever their source is maintained. For ordinary author declarations of comparable origin and importance, a later layer takes precedence over an earlier layer. Selector specificity still resolves conflicts within the winning layer when the other relevant cascade criteria match.

Declaring the order early makes it possible to understand the relationship between groups before reading all the rules. A later block named `components` adds to the existing layer. A stylesheet can also place a dependency in a named layer with the layer form of `@import`, or a build can emit its rules inside the intended layer. The delivered CSS determines the actual order.

Ordinary author rules outside named layers take precedence over ordinary layered author rules. Declarations marked `!important` reverse layer precedence. Important rules inside layers outrank important unlayered rules in the same origin, and an earlier layer can beat a later one in that case. This gives layers a clear rule set, but it means the same ordering cannot be used to reason about every declaration.

## Why it matters

Stylesheets often grow across separate owners and dependencies. A reset, a vendor widget and a local theme may all make reasonable choices in their own files. Once their rules meet on a page, repeated overrides can turn a simple choice into a contest over selector specificity. With defined layers, a site can keep the lower-priority default and let a shorter, more clearly scoped declaration win from the group that owns the decision.

The arrangement also clarifies a migration. A team can decide which rules belong to a base layer, which supply component defaults and which are site overrides. Deliberate precedence makes it easier to review a dependency update. A more specific rule from a vendor layer can remain subordinate to a normal declaration from a later component layer.

Layers are part of a larger cascade. Origin and importance still affect the result, and the relevance of a condition still matters. If the issue appears only during printing or with a user preference, first verify which declarations actually match in that environment. Layer order can explain the winner only after the browser has a set of relevant declarations.

## What it looks like in practice

Consider a project with a reset, one external component package, local components and a site theme. Write the order near the beginning of the assembled stylesheet and put the imported component package in the vendor layer. A normal theme rule for a card can override a more specific normal vendor rule for the same property when both match and the competing declarations have the same origin. A developer can keep the theme selector small and inspect the winning rule in browser tools.

Start with a small part of the stylesheet. Inventory unlayered rules and existing uses of importance before moving any of them. Record the computed styles of the component in its ordinary, focused and disabled states. Check its print presentation and its behaviour with relevant user preferences. Build the actual site and repeat the inspection against the final file, especially if the CSS bundler changes import handling.

Engineer's reduced motion case study describes finding rules that defeated an intended motion override and moving the motion rules into a later shared stylesheet part. The case illustrates how to verify an override through the whole site. For a stylesheet using layers, describe where motion defaults and preference rules belong, then check whether each component actually stops the motion it promises to stop.

A short test can exercise a competing normal declaration in two layers and a separate conflict where both declarations are important. Keep those conflicts explicit in the test. Verify the same ordering after changing the import or bundling process that produces the deployed asset.

## Where it goes wrong

Partial migrations can make old unlayered rules win over a new layered theme. Check the layer column or matching-rule details in browser tools before adding specificity. Find the declaration that won and decide whether it belongs in the layered order or stays outside it for a known reason. Then verify that the decision also works in every placement of the component.

First appearance matters when no earlier statement sets the order. Reordering imports or putting a new block above the declaration can create a different sequence from the one a reviewer expected. Put the order ahead of the participating stylesheet rules and inspect the final source. Include declared order in the shared entry point even when different files fill the groups.

Importance can create a second kind of surprise. Adding it to a preference override may allow an earlier important declaration to take priority instead. Inspect the combination of importance, origin and layer before choosing a fix. Remove conflicting importance when the component can safely operate under the normal order, or choose the precedence deliberately and check the relevant states.

A useful review leaves each property match understandable. Identify which part supplies the default, which part may override it and whether the final browser presentation serves the reader. Carry the same check through focused states, media preferences and print when those conditions are part of the component.

## Further reading
* CSS Cascading and Inheritance Level 5 on layers. Defines layer order and its effect on normal and important declarations.
* MDN's `@layer` reference. Shows syntax for declaring layers and placing rules in them.
* CSS Cascading and Inheritance Level 5 on cascade sorting. Describes how layer precedence interacts with the other cascade criteria.

=> /portfolio/made-the-reduced-motion-setting-honest/ reduced motion case study
=> https://www.w3.org/TR/css-cascade-5/#layering CSS Cascading and Inheritance Level 5 on layers
=> https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@layer MDN's `@layer` reference
=> https://www.w3.org/TR/css-cascade-5/#cascade-sort CSS Cascading and Inheritance Level 5 on cascade sorting

=> https://engineer.company/glossary/css-cascade-layer/ This page on the web
