07. Specificity Scoring, Cascade Order, Inheritance & @layer

🟡 Intermediate

1. Definition

The Cascade, Specificity, and Inheritance resolve property conflicts when multiple CSS rules target the exact same element. Keywords include inherit, initial, unset, revert, and modern Cascade Layers (@layer).

2. Why It Is Used

Understanding specificity scoring prevents CSS conflicts where rules fail to apply because another selector has higher weight. Cascade Layers (@layer) allow managing framework vs component precedence cleanly.

3. How It Works

The browser calculates a 4-part score (Inline, ID, Class, Element) for competing selectors. If scores tie, the rule declared last in the stylesheet (Order of Appearance) wins.

4. Syntax

/* Cascade Layer syntax */
@layer reset, base, components;

@layer components {
  .btn { padding: 10px; }
}

5. Specificity Scoring Reference Table

Selector CategoryScore ColumnExamplesWeight Score
Inline StylesColumn 1style="..."1, 0, 0, 0
ID SelectorsColumn 2#main-header0, 1, 0, 0
Class / Attribute / Pseudo-classColumn 3.btn, [type="text"], :hover0, 0, 1, 0
Element / Pseudo-elementColumn 4h1, p, ::before0, 0, 0, 1

6. Beginner Example

p { color: blue; }          /* Score: 0,0,0,1 */
.text-green { color: green; } /* Score: 0,0,1,0 - WINS over element! */

7. Intermediate Example

/* Inheritance keyword control */
.card-body {
  color: inherit; /* Forces child to inherit parent container color */
  border-color: initial; /* Resets border color to CSS default */
}

8. Advanced Cascade Layers Example

@layer base {
  button { background-color: #e2e8f0; color: #0f172a; }
}

@layer components {
  /* Higher layer priority overrides base layer even with equal specificity */
  .btn-primary { background-color: #2563eb; color: #ffffff; }
}

9. Line-by-Line Code Explanation

  • @layer components: Declares a cascade layer. Styles in later declared layers automatically take precedence over earlier layers regardless of selector specificity score.

10. Expected Terminal / Browser Output

Buttons and text formatted predictably based on layer precedence and calculated specificity scores.

11. Common Mistakes & Pitfalls

  • Relying on high-specificity IDs everywhere: Overusing IDs makes styles impossible to override cleanly without !important.

12. Important Rules & Limitations

  • Typography and color inherit from parents by default; box dimensions, borders, and margins do not.

13. Multilingual Explanation: English

Specificity calculates selector weight: Inline > ID > Class > Element. Equal scores resolve via order of appearance.

14. Multilingual Explanation: Hindi (Roman Script)

Specificity score: Inline > ID (#) > Class (.) > Element. Same score par baad mein likha rule jeetta hai.

15. Multilingual Explanation: Marathi (Roman Script)

Specificity rules nusar ID cha score Class peksha jast asto. Same score var nantar lihilela rule apply hoto.

16. Practice Questions

  1. Calculate the specificity score of div.card p#title.highlight.
  2. What is the purpose of Cascade Layers (@layer)?

17. Practical Coding Challenge

Create two competing CSS rules targeting a paragraph element. Use inherit on a child span to pass down its parent's blue color.

18. Related Topics