Search⌘ K
AI Features

A Paradigm-Shift for CSS Organization

Explore how CSS cascade layers reshape stylesheet organization by allowing explicit control over the cascade order. Understand the @layer at-rule to reduce specificity conflicts, improve maintainability in large codebases, and enhance browser compatibility. Discover techniques like feature detection with @supports and tools like PostCSS for robust, modern CSS workflows.

CSS cascade layers provide a new at-rule (@layer) that assists authors in managing the cascade. The @layer is a powerful tool for controlling specificity, which it does by orchestrating the order of rule sets. In an impressively coordinated cross-browser rollout, CSS cascade layers were made available from Chromium 99, Safari 15.4, and Firefox 97.

What does @layer do? It allows us, as authors, to be explicit about controlling two key elements of the “C” in CSS—the cascade. With @layer, the power is in our hands to pre-emptively mitigate conflicts between specificity and order of appearance—the last two priorities the browser considers when applying an element’s style.

CSS layers and its scopes
CSS layers and its scopes

A large codebase, or any project with multiple authors, can easily fall back on !important when specificity issues arise. With @layer , we can define groups of rule sets with a pre-determined order to reduce the likelihood of conflicts.

Consider this small example below, in which we create an ordering of layers, and then assign rules to those layers. Although the @baseline layer receives a p rule first, it still wins over the @reset layer because the initial order of layers defines the order used. ...