How do CSS cascade layers change which rules take precedence?
Quick answer
For normal declarations, later layers beat earlier layers, and unlayered declarations beat layered ones. For !important declarations, that order reverses: earlier layers beat later layers, and layered important declarations beat unlayered important ones. Layer precedence is checked before selector specificity, so a more specific selector cannot overcome a losing layer. Specificity and then source order help decide conflicts within the winning layer priority. [S1][S2]
The question, the details, the next step.
A practical checklist
Find the exact competing declarations for the same property.
Record each declaration's layer, or mark it unlayered.
Check whether either declaration is marked !important.
Compare layer priority before comparing selector specificity.
Use source order only after the relevant priority and specificity tie.
A few more useful answers
Can a highly specific selector in an earlier normal layer beat a simpler selector in a later layer?
No. Layer priority is resolved before selector specificity, so the later normal layer wins that cross-layer comparison. [S1]
Do unlayered rules always win?
No. Unlayered normal declarations outrank layered normal declarations, but layered !important declarations outrank unlayered !important declarations. [S1]
When does source order matter?
Source order can decide between declarations that remain tied at the relevant cascade priority and specificity. It cannot override a higher-priority layer. [S2]
Sources
References used for this guide.
- [S1] Cascade layers — Learn web development | MDN developer.mozilla.org · Checked
- [S2] Specificity — CSS | MDN developer.mozilla.org · Checked
