CSS Cascade and Style Priority
Goal: Understand how the browser decides which style rule to apply when multiple rules target the same element.
Cascade:
The “Cascading” in CSS means rules flow down and combine. The browser decides by:
- Source: inline > internal > external > browser defaults.
- Specificity: the more precise the selector, the higher priority.
- Order: if equal specificity, the last rule wins.
Specificity example:
- Element = 1
- Class = 10
- ID = 100
- Inline = 1000
Example:
p { color: blue; }
.text { color: green; }
#main p { color: red; }
<p id="main" class="text">Text</p> → red
Tip:
Avoid using !important or too many IDs — they make maintenance harder.