Skip to content

CSS cascade layer

a priority group for declarations in the CSS cascade

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.

  • Colourful paper wedges meeting at one point
    Photo: Engin_Akyurt / Pixabay
  • Overlapping sheets of paper in several bright colours
    Photo: blickpixel / Pixabay

Use the concept in a decision

Put “What is a CSS cascade layer?” to work in a real decision. We can help relate the concept to the system you are building or buying. Interfaces that load quickly, work on a phone and behave the same in every browser your customers happen to use.

Explore our services Get in touch

Test the assumptions behind the term

Use the explanation and linked work to frame questions for your project. We can examine the assumptions with you before a tool is chosen. Directed full‑stack GIS map development — full‑stack mapping, from database to browser

Directed full‑stack GIS map development Get in touch

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

Glossary

Choose for your actual constraints

Describe the workflow, the existing interfaces and the users who need the change. We can define a testable delivery and a handover that lets your team maintain it.

See what it costs Get in touch

Make the terminology practical

Ask how “What is a CSS cascade layer?” applies to your organisation. A concrete example from your current system is enough to start the discussion.

Explore our services
welcome@engineer.company +45 50 10 61 84 Copenhagen, Denmark
QR code for CSS cascade layer
Take this page with youhttps://engineer.company/glossary/css-cascade-layer/