Cascade layers
Definition and purpose
Cascade layers establish precedence between groups of author rules without escalating specificity. Declare a layer order early, such as reset, components, utilities. Later additions to a named layer reuse its position.
Named ordering
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @layer base, theme; @layer theme { .note { color: green; } } @layer base { #a { color: red; } } </style> </head> <body> <section class="panel"> <p id="a" class="note">Notice</p> <p id="b">Detail</p> </section> </body></html>The predeclared theme layer wins despite the later ID rule.
Syntax and mental model
Normal declarations in later layers win over earlier layers; unlayered normal declarations win over all layers. Important declarations reverse this order, including the unlayered position. An anonymous @layer block creates a distinct layer that cannot be reopened by name. revert-layer removes the current layer contribution for that property and exposes the appropriate lower-precedence result.
Important reversal
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @layer base, theme; @layer base { .note { color: navy !important; } } @layer theme { #a { color: red !important; } } </style> </head> <body> <section class="panel"> <p id="a" class="note">Notice</p> <p id="b">Detail</p> </section> </body></html>The first layer wins among important declarations.
Common mistakes
Moving !important into a later layer does not strengthen it against earlier important layers. Declare order before loading component rules.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Complete cascade sorting. Next lesson: Scope and proximity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cascade layers — knowledge check
Five questions on cascade layers.
Which normal named layer wins?
Which important layer wins?
Can a named layer be reopened?
What does an anonymous layer lack?
What does revert-layer do?