# CSS Cascade Layers (@layer) vs Shadow DOM Encapsulation and Event Retargeting

- **Canonical URL:** https://frontend.rocks/vs/css-cascade-layers-vs-shadow-dom-encapsulation/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** Cascade Layers organize light-DOM specificity; Shadow DOM creates a hard selector boundary.

## Comparison Matrix

| Dimension | CSS Cascade Layers (@layer) | Shadow DOM Encapsulation and Event Retargeting |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | CSS & Layout | DOM & Events |
| **Mental Model** | Native CSS cascade mechanism allowing explicit stylesheet priority ordering before selector specificity is even evaluated. | Isolate component CSS and DOM trees without iframes—Shadow DOM blocks outer selectors, projects light DOM via <slot>, and retargets bubbling events to the host. |
| **Prerequisites** | None | DOM Event Propagation (Capture, Target, Bubble) (Shadow trees modify event propagation by retargeting event.target to the host at the ShadowRoot.) |
| **When Not to Use** | When not to use @layer: Inside a strictly isolated Shadow DOM or CSS Nesting & @scope component without competing global stylesheets, @layer adds unnecessary indirection. | When not to use Shadow DOM: Never wrap ordinary application pages or forms in Shadow DOM, it breaks global form submission, accessibility IDs, and utility CSS unless paired with ElementInternals. |

## CSS Cascade Layers (@layer) — Under the Hood
1. During the `Recalc Style` phase of the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline), the engine sorts declarations by **Origin** (User-Agent, User, Author) and then by **Layer** in declaration order.
2. Unlayered styles form an implicit anonymous layer with **higher** priority than all named layers.
3. Selector specificity (`#id`, `.class`, `[attr]`) is only compared between rules within the same layer.

```css
@layer reset, components, utilities;

@layer components {
  /* Higher specificity (1, 1, 0), lower layer -> LOSES */
  #checkout .btn-primary { background: #1e293b; }
}

@layer utilities {
  /* Lower specificity (0, 1, 0), higher layer -> WINS */
  .bg-sky { background: #0284c7; }
}
```

## Shadow DOM Encapsulation and Event Retargeting — Under the Hood
Blink attaches a `ShadowRoot` as an encapsulated subtree whose styles and IDs are scoped away from document selectors, then merges `<slot>` placeholders with light-DOM children in the flat tree for layout. When a composed event bubbles past the `ShadowRoot` boundary, the engine rewrites `event.target` to the host element.

1. **Flat Tree Composition**: The layout engine merges `<slot>` elements with slotted light-DOM children without moving nodes in the actual DOM.
2. **Hard Style Boundary**: Document selectors stop at the `ShadowRoot`; only inheritable properties (`color`, `font`) and CSS custom properties cross inward, unlike [CSS Cascade Layers](/concepts/css-cascade-layers).
3. **Event Retargeting**: Built-in UI events (`click`, `input`) have `composed: true` and retarget `e.target` to the host; custom events default to `composed: false` and stop at the boundary.

```html
<ui-badge>
  <template shadowrootmode="open">
    <style>:host { display: inline-flex; border: 1px solid; }</style>
    <slot></slot>
  </template>
  Verified
</ui-badge>
```

## Interactive Labs & Dissections
- [CSS Cascade Layers (@layer) Lab](https://frontend.rocks/concepts/css-cascade-layers/#lab) ([Markdown](https://frontend.rocks/concepts/css-cascade-layers.md))
- [Shadow DOM Encapsulation and Event Retargeting Lab](https://frontend.rocks/concepts/shadow-dom-encapsulation/#lab) ([Markdown](https://frontend.rocks/concepts/shadow-dom-encapsulation.md))

## Primary Sources
- https://drafts.csswg.org/css-cascade-5/#layering
- https://developer.mozilla.org/en-US/docs/Web/CSS/@layer
- https://dom.spec.whatwg.org/#shadow-trees
- https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
