Complete cascade sorting
Definition and purpose
Resolve conflicts for each property on each element, not for entire rules. First exclude declarations that do not apply, then compare origin and importance, encapsulation context, element-attached styles, layer, specificity, scope proximity and source order. Each later criterion matters only when earlier criteria tie.
Filter before sorting
<!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; } @media not all { #a { color: red !important; } } .note { color: green; } </style> </head> <body> <section class="panel"> <p id="a" class="note">Notice</p> <p id="b">Detail</p> </section> </body></html>The inactive rule is irrelevant even with importance.
Syntax and mental model
From lower to higher precedence: normal UA, user, author; animations; important author, user, UA; active transitions. Within an origin, normal outer encapsulation contexts beat inner ones; important inner contexts beat outer ones. Normal inline styles outrank author style rules within the same context, but important declarations can beat normal inline styles. Layers sort before selector specificity.
Layer before specificity
<!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 { #a { color: red; } } .note { color: navy; } </style> </head> <body> <section class="panel"> <p id="a" class="note">Notice</p> <p id="b">Detail</p> </section> </body></html>Normal unlayered author rules outrank normal layered rules.
Common mistakes
Do not count selector IDs until the earlier criteria tie. A declaration in an inactive media query cannot compete at all.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Semantic tokens and themes, Highlights and specialized states. Next lesson: Cascade layers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Complete cascade sorting — knowledge check
Five questions on complete cascade sorting.
What is excluded before cascade sorting?
Which sorts before specificity?
Normal author versus normal user origin?
Which context wins for important declarations in the same origin?
What is compared after equal specificity in scoped rules?