CSS Cascade Layers Visualizer

Understand and debug CSS cascade layers by defining your @layer stack, writing rules in each layer, and watching the visualizer explain exactly which declaration wins — and why. Cascade layers change the rules: a low-specificity rule in a later layer beats a high-specificity rule in an earlier layer.

How to use the CSS Cascade Layers Visualizer

1. Enter the layer names in priority order (lowest first, highest last) in the Layer order field. 2. Enter the target CSS selector. 3. For each layer, a textarea appears — type one CSS declaration per line (e.g. color: red). 4. Click Visualize. The tool shows the winning declaration for each property along with which layer it came from, and emits the full @layer CSS block. Note: unlayered rules (not shown here) always beat layered rules — the visualizer only compares within the defined layers. The Example button loads a classic reset/base/components/utilities scenario targeting .btn.

How CSS cascade layers change specificity resolution

Before @layer (CSS Cascading and Inheritance Level 5), the cascade was determined by origin (author/user/UA), importance, specificity, and source order. High-specificity rules in a component library would trample utility classes, forcing workarounds like !important or specificity hacks. In large codebases this created an arms race of ever-longer selectors.

Cascade layers introduce an explicit priority ordering. You declare the stack once — @layer reset, base, components, utilities; — and every rule in a later layer wins over every rule in an earlier layer, regardless of specificity. A utility class .mt-4 with a single class selector (0,1,0) in the utilities layer beats a component rule .card .btn (0,2,0) in the components layer. Specificity is only a tiebreaker within the same layer.

Unlayered styles (rules outside any @layer block) always win over layered styles, making it safe to add third-party layered libraries without fighting their specificity. !important reverses the layer order — an !important rule in an earlier layer beats one in a later layer, which is intentional for user-override scenarios. Cascade layers are Baseline 2022 and supported in all major browsers.

Common use cases

  • Third-party resets — wrap a CSS reset like Normalize.css in @layer reset so it never overrides your own base styles, eliminating all reset-specificity fights.
  • Design-system isolation — layer component library CSS below utilities so a simple .hidden class always wins without needing !important.
  • Feature flags in CSS — conditionally include an @layer block (e.g. from a server response) to activate a theme variant without touching existing specificity.
  • Safe third-party imports — import external stylesheets into a named layer (@import url("...") layer(vendor)) so their specificity is fully sandboxed.
  • Incremental migration — gradually move CSS files into layers over time — unlayered legacy CSS keeps winning, giving you a safe migration path.

Frequently asked questions

Do unlayered styles always beat layered styles?

Yes — any rule written outside an @layer block has higher priority than any layered rule, regardless of specificity. This is intentional: author styles you write directly will not be overridden by a layered library.

How does !important interact with layer order?

!important reverses the layer order. An !important rule in the lowest-priority layer (e.g. reset) beats !important in the highest-priority layer (e.g. utilities). This mirrors the original cascade intent: user !important overrides author !important.

Can I nest layers inside other layers?

Yes. @layer base { @layer typography { ... } } creates base.typography. The full dotted name is used in the outer layer order declaration if you need to reference it explicitly.

What happens if I declare @layer order after a rule uses that layer?

The @layer statement is hoisted — the first mention of each layer name establishes its position. Subsequent @layer declarations at the same nesting level append new layers at the end. Best practice: declare the full order once at the top of your stylesheet.

Is @layer supported in all browsers?

Yes — Chrome 99, Firefox 97, and Safari 15.4 all shipped support. It is Baseline 2022. All modern browsers support it; Internet Explorer does not.