# Cascade Layers (@layer)

> Organize your CSS into ordered layers. Solve the specificity war by deciding which category of styles wins.

Subject: CSS | Lesson 39 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/cascade-layers
Previous: [:has() The Parent Selector](https://codewithanimation.in/css/has-parent-selector.md) | Next: [Anchor Positioning & color-mix()](https://codewithanimation.in/css/anchor-positioning.md)

## Fixing the specificity war

Historically, the CSS cascade relied heavily on specificity. An ID selector (`#button`) would always defeat a class selector (`.btn`), even if the class was written much later in the file.

This led to 'specificity wars', where developers had to write increasingly complex selectors (or use `!important`) just to force their styles to apply.

Cascade Layers (`@layer`) introduce a new level to the cascade. They allow you to group styles into named layers and explicitly declare the priority order of those layers, completely bypassing traditional specificity rules.

## Predictable style overrides

By defining layer order up front (e.g., `@layer reset, base, components, utilities;`), the browser guarantees that styles in `utilities` will always override styles in `components`.

Even if a rule in the `components` layer uses a heavy ID selector, a simple class selector in the `utilities` layer will win, because the layer priority is evaluated before selector specificity.

This makes integrating third-party libraries trivial: you simply load the library into a low-priority `vendor` layer, and your own `components` layer will cleanly override it without fighting its specificity.

## Defining and populating layers

First, establish the order. The layer listed last has the highest priority: `@layer reset, theme, components;`.

Then, populate the layers by wrapping your CSS rules in `@layer` blocks. ` @layer theme { .card { background: dark; } } `.

You can also import entire external stylesheets directly into a layer using `@import url('theme.css') layer(theme);`.

## Common mistake: Unlayered styles always win

The most common point of confusion is how layers interact with traditional CSS. Any normal CSS rule that is *not* placed inside a layer is called 'unlayered' style.

Unlayered styles have the highest priority in the normal cascade. They will defeat all layered styles, regardless of layer order or specificity. If you migrate to layers, you must be careful not to leave legacy styles unlayered, or they will stubbornly override your new layered architecture.

## Tip: Nested layers

Layers can be nested. If you have `@layer framework`, you can create a sub-layer via `@layer framework.reset`. This helps large teams isolate their architecture without clashing with the global namespace.

## Try it: Try it

Reorder the layer declaration below. Watch how the winning style completely ignores traditional specificity.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/cascade-layers

## Check yourself

### Question 1

Given `@layer base, utilities;`, if `.card` in `utilities` sets `color: red` and `#hero-card` in `base` sets `color: blue`, which color wins?

- A. Red
- B. Blue

Correct answer: A

Layer order is evaluated before selector specificity. Since `utilities` comes after `base` in the declaration, it has higher priority. Red wins.

- Why not B: Even though `#hero-card` has higher specificity, it belongs to the `base` layer. The `utilities` layer was declared later, so it wins.

### Question 2

What happens if a style is written without an `@layer` block (unlayered)?

- A. It wins over all normal layered styles.
- B. It is treated as the lowest priority.
- C. It only applies if there are no layers defined.
- D. It throws a CSS parse error.

Correct answer: A

Normal unlayered styles are injected at the very end of the layer cascade, giving them priority over everything defined inside layers.

- Why not B: By default, unlayered normal styles defeat all layered normal styles.
- Why not C: It applies alongside layers but takes precedence over them.
- Why not D: Unlayered styles are perfectly valid; they represent traditional CSS behavior.

### Question 3

How can you load an external stylesheet directly into a specific layer?

- A. @import url('styles.css') layer(base);
- B. <link rel="stylesheet" href="styles.css" layer="base">
- C. @layer base { @import url('styles.css'); }
- D. You cannot layer external files.

Correct answer: A

The `layer()` function extends the `@import` statement, allowing you to sandbox an entire external stylesheet (like Bootstrap or Tailwind) into a specific layer.

- Why not B: While an HTML attribute is proposed for the future, the current standard way is via CSS `@import`.
- Why not C: `@import` statements must be at the top level of a file, they cannot be nested inside `@layer` blocks.
- Why not D: You can layer external files using the `layer()` function in an `@import` statement.

## Remember this

- Cascade Layers bypass specificity. A rule in a higher-priority layer always wins.
- Layer priority is defined by declaration order. The last declared layer wins.
- Normal CSS written outside of any layer (unlayered) will defeat all layered styles.
- You can import external stylesheets directly into a layer using `@import`.
