liveBook · Manning

Cascade layer

As explained in CSS in Depth, Second Edition

A nested cascade layer is a cascade layer defined within another cascade layer and ordered relative to its sibling layers.

As a stylesheet grows, one layer may contain too many styles to organize comfortably. A nested cascade layer divides that layer into smaller groups, with each child layer ordered relative to its siblings. The modules layer is a candidate: it often holds most of a site's reusable component styles. section 8.1.2 section 8.2.5

@layer modules {
  @layer layout { ... }
  @layer theme { ... }
}

The layout and theme layers are declared inside modules. The same nested layers can also be written with dotted names, such as @layer modules.layout and @layer modules.theme. section 8.1.2

Layer priority follows the order in which layers first appear. Later layers take precedence over earlier ones when normal declarations conflict, before selector specificity is considered. Referencing an existing layer again adds styles without moving it in the priority order. For declarations marked !important, layer priority reverses. section 8.1.1 section 8.1.2

Use nested layers when a large parent layer needs a deliberate internal priority order, not simply because every component needs its own group. There is no established strategy for subdividing the modules layer; choose names and boundaries that make the stylesheet easier to change. section 8.2.5 section 8.1.2

How CSS in Depth, Second Edition covers it

How deep it goes
Shows nested and dotted @layer syntax and explains layer ordering, but offers no established scheme for subdividing modules.
Examples
Uses layout and theme nested inside modules, alongside navigation-menu and card styles as examples of module code.
What it assumes
Assumes familiarity with CSS rules and selectors; introduces @layer syntax and its place before specificity in the cascade.
What you can do after
Operate/configure nested @layer rules to organize styles within a parent layer while controlling sibling priority.
How it connects
Connects cascade layers to selector specificity, source order, !important, and reusable CSS components.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.