How does !important affect specificity?
!important does not increase a selector's specificity, but overrides the cascade's result by placing itself at the very top of the priority list. In other words, it is not about the selector's "weight", but about the rule's "strength".
How priority works with !important
In effect, !important creates a separate, highest level, above specificity. The order in which rules are applied becomes:
!important- inline styles
idselector- class, attribute, pseudo-class selector
- tag selector
- browser styles
Example
p { color: blue !important; }
#text { color: red; }Although #text has higher specificity, the final color will be blue, because the first rule has !important.
When !important still loses
Only in one case, if there is another !important with a higher-weight selector:
p { color: blue !important; }
#text { color: red !important; }Here red will win, because with equal !important, specificity takes over again, id is stronger than p.
Summary
!importantdoes not change specificity- but affects the final priority, bypassing the cascade and the selector's "weight"
- when two
!importantrules conflict, the comparison again goes by selector specificity
This mechanism makes !important a "cheat code", which is why it is avoided in regular development.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.