Back to the thread left dangling in Section 1.2: as projects grow and style sources multiply (reset styles, component libraries, page overrides, third-party styles), selector specificity alone soon stops being manageable. @layer and @scope are the modern CSS answer to this "scaling" problem.
The Specificity Predicament in Large Projects
Picture this: a component library defines the styles for .button, and page-level code wants to override its color — but because the library internally uses a more specific selector (such as .card .button), the page styles can never win. The only way out is to keep inflating the specificity or abuse !important, and the style sheet gradually spirals out of control.
@layer Basics
@layer divides styles into "cascade layers" with a well-defined priority order between layers, completely independent of the selector specificity inside each layer — a later-declared layer overrides earlier layers as a whole, no matter how low its selectors' specificity is.
/* Step 1: declare the layer order (later layers win) */@layerreset, base, components, utilities;
/* Step 2: put styles into their corresponding layers */@layerreset {
* { margin: 0; }
}
@layercomponents {
.card .button { color: gray; } /* higher specificity, but a lower layer */
}
@layerutilities {
.button { color: teal; } /* lower specificity, but a higher layer — it wins */
}
Key ruleAny explicitly layered rule beats unlayered normal rules — but all of them lose to inline styles and !important (and !important reverses the order between layers: an !important in an earlier-declared layer actually wins).
Layer Declaration Order Is the Priority Order
A layer's position in the cascade order is fixed the first time its name appears; afterwards you can append more @layer blocks with the same name anywhere in the file, and their contents merge without changing the order — which makes it possible to "plan the layer structure first, then fill in content in batches", ideal for splitting maintenance across multiple CSS files.
/* reset.css */@layerreset { ... }
/* components.css — loading after reset.css is fine */@layercomponents { ... }
@scope: Restricting Where Styles Apply
@scope solves a different problem: it keeps a set of styles effective only within a specified DOM subtree, so they never "leak" into unrelated areas — a natural way to fence off components that may be nested and reused:
@scope (.card) to (.card-nested) {
p { color: #5c5872; }
}
/* applies only to p inside .card, and stops descending at .card-nested — so it never affects another card nested inside */
A Look Back at the Whole Book
From the simplest tag selector to pseudo-class combinations that express complex component relationships; from the static box model to Flexbox, Grid and media/container queries that respond to viewport and container changes; and on to color, gradients, motion and the variable system — these seven chapters form the core skeleton of modern CSS. Real fluency comes from combining them to solve concrete problems: as a next step, pick a page you are building (or want to build) and try to construct it from scratch using only what this book has covered.